您的位置:首页 > 编程语言 > Java开发

springmvc集成shrio

2016-05-05 17:36 429 查看
直接上代码

web.xml

<!-- 配置Shiro过滤器,先让Shiro过滤系统接收到的请求 -->
<!-- 这里filter-name必须对应applicationContext.xml中定义的<bean id="shiroFilter" /> -->
<!-- 使用[/*]匹配所有请求,保证所有的可控请求都经过Shiro的过滤 -->
<!-- 通常会将此filter-mapping放置到最前面(即其他filter-mapping前面),以保证它是过滤器链中第一个起作用的 -->
<!-- targetFilterLifecycle该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->
<!---->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


springmvc.xml

<!-- 当前对象用于创建shiro框架需要的过滤器对象 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 注入安全管理器 -->
<property name="securityManager" ref="securityManager"></property>

<!-- 登录成功之后跳转的页面,一般这个逻辑都是自己控制的,所以此处不需要 <property name="successUrl" value="/WEB-INF/views/error/success.jsp"></property> -->
<!-- 如果用户尚未登录,跳转到的界面 -->
<property name="loginUrl" value="/loginUrl"></property>
<!-- 如果未授权,跳转到的界面 -->
<property name="unauthorizedUrl" value="/error"></property>

<property name="filterChainDefinitions">
<value>
<!--访问权限配置,admin角色才可以操作/suiyuan/page -->
/suiyuan/page = authc,roles[admin]
</value>
</property>
</bean>

<!-- 定义安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 注入realm -->
<property name="realm" ref="userRealm"></property>
</bean>

<!-- 自定义Realm -->
<bean id="userRealm" class="com.suiyuan521.cms.web.authority.UserRealm">
</bean>
<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

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

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>


自定义Realm

@Component("userRealm")
public class UserRealm extends AuthorizingRealm {

@Autowired
private UserMapper userDao;

/**
* 授权方法
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// 获取当前登录的用户名,等价于(String)principals.fromRealm(this.getName()).iterator().next()
String currentUsername = (String) super.getAvailablePrincipal(principals);
List<String> roleList = new ArrayList<String>();
List<String> permissionList = new ArrayList<String>();
// 如果是suiyuan用户,增加角色和权限
if(currentUsername.equals("suiyuan")) {
roleList.add("admin");
permissionList.add("suiyuan/page");
} else {
roleList.add("normal");
permissionList.add("normal");
}

// 为当前用户设置角色和权限
SimpleAuthorizationInfo simpleAuthorInfo = new SimpleAuthorizationInfo();
simpleAuthorInfo.addRoles(roleList);
simpleAuthorInfo.addStringPermissions(permissionList);
return simpleAuthorInfo;
}

/**
* 认证方法
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
// 获取基于用户名和密码的令牌
// 实际上这个authcToken是从LoginController里面currentUser.login(token)传过来的
// 两个token的引用都是一样的
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
System.out.println("验证当前Subject时获取到token为"
+ ReflectionToStringBuilder.toString(token, ToStringStyle.MULTI_LINE_STYLE));
List<SysUser> allExistUser = this.userDao.getByName(token.getUsername());
int allExistUserSize = allExistUser.size();
SysUser existUser = null;
// 用户不存在
if (allExistUserSize == 0) {
throw new UnknownAccountException();
} else if (allExistUserSize > 1) {
// 用户重复
throw new UnknownAccountException();
} else {
existUser = allExistUser.get(0);
}

String existUserPass = existUser.getPassword();
if (existUserPass.equals(String.valueOf(token.getPassword()))) {
AuthenticationInfo authcInfo =
new SimpleAuthenticationInfo(existUser.getName(), existUser.getPassword(), this.getName());
setSession("userName", existUser.getName());
setSession("isLogin", true);
return authcInfo;
} else {
throw new IncorrectCredentialsException();
}
}

/**
* 将一些数据放到ShiroSession中,以便于其它地方使用
*
* @see 比如Controller,使用时直接用HttpSession.getAttribute(key)就可以取到
*/
private void setSession(Object key, Object value) {
Subject currentUser = SecurityUtils.getSubject();
if (null != currentUser) {
Session session = currentUser.getSession();
if (null != session) {
session.setAttribute(key, value);
}
}
}

}


在Controller中如何使用注解?

方法前加上:@RequiresRoles("admin")


问题:

1、对于注解类,没有权限的时候会报错,而不是跳转到指定界面,如何处理? 可以做全局异常处理。参考网页:http://my.oschina.net/sniperLi/blog/650627?p={{currentPage-1}}

2、二次代理问题的解释及处理方法:http://jinnianshilongnian.iteye.com/blog/1894465
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: