shiro 不执行授权方法 doGetAuthorizationInfo()

本文介绍了一个使用ShiroDbRealm实现的身份验证与授权过程。通过整合Spring和Shiro框架,文章详细展示了如何自定义Realm来处理用户登录及权限检查,并配置了ShiroFilter拦截器以实现细粒度的安全控制。
[size=x-large][color=black][b]ShiroDbRealm.java 代码如下[/b][/color][/size]

public class ShiroDbRealm extends AuthorizingRealm {

@Resource
private UserService userService;

/**
* 认证回调函数,登录时调用.
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
User currentUser = userService.findByUserName(token.getUsername());
if (currentUser != null) {
if (currentUser.getStatus()==User.STATUS_DISABLED) {
throw new DisabledAccountException("用户已注销");
}else if(currentUser.getStatus()==User.STATUS_NOT_ACTIVE){
throw new DisabledAccountException("用户未激活");//这里需要编写一个用户未激活异常
}
return new SimpleAuthenticationInfo(currentUser.getUsername(),currentUser.getUserpwd(), "");
}
return null;
}

/**
* 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
// Cache<Object, AuthenticationInfo> authenticationCache =
// getAuthenticationCache();
String primaryPrincipal = (String) principals.getPrimaryPrincipal();
System.out.println("-----------*************************------------>"+ primaryPrincipal);
List<String> roles = new ArrayList<String>();
List<String> permissions = new ArrayList<String>();

SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

User user = userService.findByUserName(primaryPrincipal);
if(user != null){
for (Role role : user.getRoles()) {
roles.add(role.getName());
for (Permission p : role.getPermissions()) {
permissions.add(p.getPrivilege());
}
}
}else{
throw new AuthorizationException();
}
//给当前用户设置角色
info.addRoles(roles);
//给当前用户设置权限
info.addStringPermissions(permissions);

return info;
}
}


[size=x-large][color=black][b]applicationContext-shiro.xml配置[/b][/color][/size]

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

<description>Shiro安全配置</description>

<!-- Shiro's main business-tier object for web-enabled applications -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="shiroDbRealm"/>
<property name="cacheManager" ref="shiroEhcacheManager"/>
</bean>

<!-- 項目自定义的Realm -->
<bean id="shiroDbRealm" class="com.wsq.app.service.common.ShiroDbRealm">
<!-- <property name="userService" ref="userService"/> 这里我在项目中只用了注解注入-->
</bean>


<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 这个属性是必须的 -->
<property name="securityManager" ref="securityManager"/>
<!-- 没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面 -->
<property name="loginUrl" value="/login"/>
<!-- 登录成功默认跳转页面,不配置则跳转至”/”。如果登陆前点击的一个需要登录的页面,则在登录自动跳转到那个需要登录的页面。不跳转到此 -->
<property name="successUrl" value="/"/>
<!-- 没有权限默认跳转的页面 -->
<property name="unauthorizedUrl" value=""/>
<!-- 就是需要验证的地址的列表,常用的包含anon、authc、perms、roles、user、logout。 -->
<property name="filterChainDefinitions">
<value>
/static/** = anon
/login = anon
/login/** = anon
/logout = user
/** = authc
</value>
</property>
</bean>

<!-- 用户授权信息Cache, 采用EhCache -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:resource/ehcache-shiro.xml"/>
</bean>

<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- AOP式方法级权限检查 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"/>
</bean>

<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>

</beans>


[size=x-large][color=black][b]web.xml配置[/b][/color][/size]

 <!-- Shiro Security 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>


[size=medium][color=red][b]在spring-mvc的配置文件中,添加[/b][/color][/size]

[quote]<aop:config proxy-target-class="true"></aop:config>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>[/quote]

参考:[url]http://ask.youkuaiyun.com/questions/201357[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值