我是靠谱客的博主 美好魔镜,最近开发中收集的这篇文章主要介绍shiro与SSM整合之授权和注解式开发1、shiro授权角色、权限2、Shiro的注解式开发,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
文章目录
- 1、shiro授权角色、权限
- 权限表设计图:
- ShiroUserMapper:
- ShiroUserMapper.xml:
- MyRealm:
- 2、Shiro的注解式开发
- 常用注解介绍:
- ShiroUserController:
- 在springmvc-servlet.xml添加:
- jsp中添加:
- 测试:
1、shiro授权角色、权限
权限表设计图:
ShiroUserMapper:
Set<String> getRolesByUserId(Integer uid);
Set<String> getPersByUserId(Integer uid);
ShiroUserMapper.xml:
<select id="getRolesByUserId" resultType="java.lang.String" parameterType="java.lang.Integer">
select r.roleid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role r
where u.userid = ur.userid and ur.roleid = r.roleid
and u.userid = #{uid}
</select>
<select id="getPersByUserId" resultType="java.lang.String" parameterType="java.lang.Integer">
select p.permission from t_shiro_user u,t_shiro_user_role ur,t_shiro_role_permission rp,t_shiro_permission p
where u.userid = ur.userid and ur.roleid = rp.roleid and rp.perid = p.perid
and u.userid = #{uid}
</select>
MyRealm:
前面讲了认证,这次只重写了授权方法
/**
* 授权
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
ShiroUser shiroUser = this.shiroUserService.queryByName(principalCollection.getPrimaryPrincipal().toString());
Set<String> roleIds = this.shiroUserService.getRolesByUserId(shiroUser.getUserid());
Set<String> perIds = this.shiroUserService.getPersByUserId(shiroUser.getUserid());
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.setRoles(roleIds);
info.setStringPermissions(perIds);
return info;
}
然后在测试就好了!
2、Shiro的注解式开发
常用注解介绍:
@RequiresAuthenthentication:表示当前Subject已经通过login进行身份验证;即 Subject.isAuthenticated()返回 true
@RequiresUser:表示当前Subject已经身份验证或者通过记住我登录的
@RequiresGuest:表示当前Subject没有身份验证或者通过记住我登录过,即是游客身份
@RequiresRoles(value = {"admin","user"},logical = Logical.AND):表示当前Subject需要角色admin和user
@RequiresPermissions(value = {"user:delete","user:b"},logical = Logical.OR):表示当前Subject需要权限user:delete或者user:b
ShiroUserController:
/**
* 身份认证
* @param req
* @param resp
* @return
*/
@RequestMapping("/passUser")
public String passUser(HttpServletRequest req, HttpServletResponse resp){
return "admin/addUser";
}
/**
* 角色认证
* @param req
* @param resp
* and : 必须同时具备
* or:有其一即可
* @return
*/
@RequiresRoles(value = {"1","4"},logical = Logical.AND)
@RequestMapping("/passRole")
public String passRole(HttpServletRequest req, HttpServletResponse resp){
return "admin/listUser";
}
/**
* 权限认证
* @param req
* @param resp
* @return
*/
@RequiresPermissions(value = {"user:update","user:view"},logical = Logical.OR)
@RequestMapping("/passPer")
public String passPer(HttpServletRequest req, HttpServletResponse resp){
return "admin/resetPwd";
}
/**
* 认证失败后的处理请求方式
* @param req
* @param resp
* @return
*/
@RequestMapping("/unauthorized")
public String unauthorized(HttpServletRequest req, HttpServletResponse resp){
System.out.println("错误认证处理!!!");
return "unauthorized";
}
在springmvc-servlet.xml添加:
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"></property>
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">
unauthorized
</prop>
</props>
</property>
<property name="defaultErrorView" value="unauthorized"/>
</bean>
jsp中添加:
<ul>
shiro注解
<li>
<a href="${pageContext.request.contextPath}/passUser">用户认证</a>
</li>
<li>
<a href="${pageContext.request.contextPath}/passRole">角色</a>
</li>
<li>
<a href="${pageContext.request.contextPath}/passPer">权限认证</a>
</li>
</ul>
测试:
最后
以上就是美好魔镜为你收集整理的shiro与SSM整合之授权和注解式开发1、shiro授权角色、权限2、Shiro的注解式开发的全部内容,希望文章能够帮你解决shiro与SSM整合之授权和注解式开发1、shiro授权角色、权限2、Shiro的注解式开发所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复