shiro成熟的例子还是很少,貌似springSide4 有个完整的例子。附件有简单数据库设计。
package com.myShiro.test;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class JdbcReamTest {
public static void main(String[] args) {
System.out.println("Hello shiro!");
MysqlDataSource datasource = new MysqlDataSource();
datasource.setUser("root");
datasource.setPassword("root");
datasource.setServerName("localhost");
// datasource.setDriverClassName("com.mysql.jdbc.Driver");
datasource.setUrl("jdbc:mysql://localhost:3306/test");
// datasource.setMaxActive(10);
org.apache.shiro.realm.jdbc.JdbcRealm jdbcRealm = new JdbcRealm();
jdbcRealm.setDataSource(datasource);
jdbcRealm.setPermissionsLookupEnabled(true);
jdbcRealm
.setAuthenticationQuery("SELECT PASSWORD FROM account WHERE name = ?");
jdbcRealm
.setUserRolesQuery("SELECT NAME FROM role WHERE id =(SELECT roleId FROM account_role WHERE userId = (SELECT id FROM account WHERE NAME = ?))");
jdbcRealm
.setPermissionsQuery("SELECT NAME FROM permission WHERE id in (SELECT permissionId FROM permission_role WHERE (SELECT id FROM role WHERE NAME = ?))");
DefaultSecurityManager security = new DefaultSecurityManager(jdbcRealm);
SecurityUtils.setSecurityManager(security);
Subject currentUser = SecurityUtils.getSubject();
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("ynp",
"111111");
token.setRememberMe(true);
try {
currentUser.login(token);
System.out.println("login successfully");
} catch (UnknownAccountException uae) {
System.out.println("There is no user with username of "
+ token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
System.out.println("Password for account "
+ token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
System.out.println("The account for username "
+ token.getPrincipal() + " is locked. " +
"Please contact your administrator to unlock it.");
}
// ... catch more exceptions here (maybe custom ones specific to
// your application?
catch (AuthenticationException ae) {
// unexpected condition? error?
}
}
// say who they are:
// print their identifying principal (in this case, a username):
System.out.println("User [" + currentUser.getPrincipal()
+ "] logged in successfully.");
// test a role:
if (currentUser.hasRole("admin")) {
System.out.println("May the admin be with you!");
} else {
System.out.println("Hello, mere mortal.");
}
// test a typed permission (not instance-level)
if (currentUser.isPermitted("write")) {
System.out.println("You can write!.");
} else {
System.out.println("Sorry, lightsaber rings are for schwartz masters only.");
}
// a (very powerful) Instance Level permission:
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
System.out
.println("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. "
+
"Here are the keys - have fun!");
} else {
System.out
.println("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
}
// all done - log out!
currentUser.logout();
}
}
分享到:
相关推荐
shiro反序列化测试工具包,两个使用任意一个即可
一个简单的SSM框架,基于IDEA IDE开发,可以直接运行,里面加入了shiro,前端用bootstrap展现
shiro框架的权限测试代码 ,里面有关于shiro的一些简单应用,仅供研究。
shiro的一个小例子,用来学习借鉴,可能会有所帮助
eclipse+oracle+jfinal+shiro+sql自己参考网上资料整理!
( shiro-example编程例子.zip )( shiro-example编程例子.zip )( shiro-example编程例子.zip )
跟我学shiro的书和例子源码,非常适合学习shiro者使用
本测试小例子是用eclipse编写的使用shiro内置的jdbcRealm的测试数据库,免费提供,然后直接解压缩,使用eclipse直接open或者import打开,然后编译运行就可以了
spring整合shiro的一个登录小例子,资源包括源代码以及JAR包,
Shiro的一个demo
使用spingmvc、shiro权限框架做的一个小例子,例子是基于简单的SpringMvc和shiro的配置完成,动态分配权限也写的相对简单,主要是在RememberMe中想在关闭网页后下次自动获取登录这点本人比较模糊,希望有大虾指点...
本资源包含shiro相关实现例子以及对应的shiro教程文档,希望能对通道中人有所帮助
供大家参考,由于本人工作时间紧,所以这个项目拖了将近一个月的时间,大体的权限管理已经实现了,只是部分修改和删除功能没有时间去写了,不过shiro的权限控制已经可以提现了,希望对大家有所帮助吧,尤其是使用SSH...
Apache Shiro是Java的一个安全框架。功能强大,使用简单的Java安全框架,它为开发人员提供一个直观而全面的认证,授权,加密及会话管理的解决方案。 实际上,Shiro的主要功能是管理应用程序中与安全相关的全部,...
一: 项目简介:此项目只是简单的集成spring+springmvc+shiro+ehcahce 二: 步骤说明: 1:项目集成spring 在web.xml中配置spring容器的监听器。... 此例子是给一哥们理解的,一些注释和代码未做删减。
一个Shiro的简单例子Pom依赖 <dependency> <groupId>org.apache.shiro</groupId> <artifac
Apache Shiro 身份认证例子-源码
shiro的功能很强大,于是学习学习。感谢一个老外,参照他的代码,把它的maven工程改成了我们通常的eclipse web工程(有jar包),做了些修改。在eclipse里导入即可跑起来。数据库在包里面。(禁止用作商业用途)。
非常完美的spring+springMVC+shiro 完美例子实现权限认证,相信你一定会喜欢,里面有文档说明