一、在web.xml中添加shiro过滤器
<!-- Shiro filter-->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二、在Spring的applicationContext.xml中添加shiro配置
1、添加shiroFilter定义
<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/user/list" />
<property name="unauthorizedUrl" value="/login" />
<property name="filterChainDefinitions">
<value>
/login = anon
/user/** = authc
/role/edit/* = perms[role:edit]
/role/save = perms[role:edit]
/role/list = perms[role:view]
/** = authc
</value>
</property>
</bean>
2、添加securityManager定义
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm" />
</bean>
3、添加realm定义
<bean id=" myRealm" class="com...MyRealm" />
三、实现MyRealm:继承AuthorizingRealm,并重写认证授权方法
public class MyRealm extends AuthorizingRealm{
private AccountManager accountManager;
public void setAccountManager(AccountManager accountManager) {
this.accountManager = accountManager;
}
/**
* 授权信息
*/
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
String username=(String)principals.fromRealm(getName()).iterator().next();
if( username != null ){
User user = accountManager.get( username );
if( user != null && user.getRoles() != null ){
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
for( SecurityRole each: user.getRoles() ){
info.addRole(each.getName());
info.addStringPermissions(each.getPermissionsAsString());
}
return info;
}
}
return null;
}
/**
* 认证信息
*/
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken ) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
String userName = token.getUsername();
if( userName != null && !"".equals(userName) ){
User user = accountManager.login(token.getUsername(),
String.valueOf(token.getPassword()));
if( user != null )
return new SimpleAuthenticationInfo(
user.getLoginName(),user.getPassword(), getName());
}
return null;
}
}
分享到:
相关推荐
本项目是基于Spring、Spring MVC、MiniJdbc和Shiro开发的权限管理系统LarvaFrame设计源码,包含436个文件,其中包括145个Java源文件,92个JavaScript文件,30个PNG图片,30个CSS样式文件,27个GIF图片,24个XML文件...
spring mvc、apache shiro、mysql 框架搭建,基于maven构建
基于maven构建 spring mvc + apache shiro + mysql 框架搭建;有任何不懂的技术问题都可以联系我共同讨论
权限管理系统,基于 shiro,spring mvc,mybatis 框架开发
spring mvc、mybatis、bootstrap、shiro 框架整合开发网上求职招聘毕业设计系统、带mysql数据库
spring、springmvc、shiro集成空框架,包括jar包、配置文件。
JAVA spring mvc + mybatis + shiro HTML5 bootstrap前后台框架搭建
开发网上求职招聘毕业设计系统 spring mvc+mybatis+bootstrap+shiro 框架整合、带mysql数据库;有任何不懂的技术问题都可以联系我
spring mvc、mybatis、bootstrap、shiro 框架整合开发网上求职招聘毕业设计系统、带mysql数据库.zip
因为项目过大,所以将项目所需要的jar包全部放在百度云盘中,云盘链接:https://pan.baidu.com/s/1kUXlPrh 如果有问题请及时留言,本人运行没有问题的
JeeSite是在Spring Framework基础上搭建的一个Java基础开发平台,以Spring MVC为模型视图控制器,MyBatis为数据访问层, Apache Shiro为权限授权层,Ehcahe对常用数据进行缓存,Activit为工作流引擎。是JavaEE界的...
Java企业级权限系统,可供学习Spring Security权限框架、 Apache Shiro权限框架、Spring MVC、 RBAC模型、模块开发等等,内有详细视频教程,由于上传大小限制,可联系免费获取!
shiro 是java的一款安全管理框架,支持用户认证、授权、会话管理等等,灵活性强 该DEMO是shiro在springmvc中的整合,简单易学
springMVC+mybatis+shiro+redis 项目整合demo。
Spring mvc、 mybatis、 easyui、 ueditor、 shiro 权限框架开发吉客多后台管理系统
springmvc集成shiro安全框架,实现用户身份认证、权限管理
加入安全框架 shiro (登录授权)(session管理) 11.根据汉字 解析汉字的全拼(拼音)和首字母(导入excel到用户表,根据用户的汉字姓名生成拼音的用户名) 12.app接口(支持与其它语言数据交互) 12.极光推送 (推送给APP...
Shiro安全框架, 实现系统的单点登陆和登出功能, 包含数据库文件
SpringMVC MVC框架 Spring Framework 容器 Apache Shiro 安全框架 Mybatis ORM框架 MyBatis Generator 代码生成 C3P0 数据库连接池 Ehcache 进程内缓存框架 Apache poi 文件导入导出 Maven 项目构建管理 ...