目录
上次我们分享了Shiro框架的认证功能,在这里我们继续分享Shiro框架的功能之一:授权
- <select id="getRolesByUserId" resultType="java.lang.String" parameterType="java.lang.String">
- SELECT roleid FROM t_shiro_user_role ur,t_shiro_user u where ur.userid=u.userid and u.username=#{userName}
- select>
-
- <select id="getPersByUserId" resultType="java.lang.String" parameterType="java.lang.String">
- SELECT rp.perid FROM t_shiro_user u,
- t_shiro_user_role ur,
- t_shiro_role_permission rp
- where ur.userid=u.userid
- and ur.roleid=rp.roleid
- and u.username= #{userName}
- select>
- Set
getRolesByUserId(@Param("userName") String userName); -
- Set
getPersByUserId(@Param("userName") String userName);
- Set
getRolesByUserId(String userName); -
- Set
getPersByUserId(String userName);
- @Override
- public Set
getRolesByUserId(String userName) { - return userMapper.getRolesByUserId(userName);
- }
-
- @Override
- public Set
getPersByUserId(String userName) { - return userMapper.getPersByUserId(userName);
- }
业务流程:
1) 获取验证身份(用户名)
2) 根据身份(用户名)获取角色和权限信息
3) 将角色和权限信息设置到SimpleAuthorizationInfo
- @Override
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
- String userName = principals.getPrimaryPrincipal().toString();//获取账户名
- Set
roleIds = userBiz.getRolesByUserId(userName); - Set
perIds = userBiz.getPersByUserId(userName); - SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
- // 将当前登录的权限交给shiro的授权器
- info.setStringPermissions(perIds);
- // 将当前登录的角色交给shiro的授权器
- info.setRoles(roleIds);
- return info;
- }
@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
身份认证:requireUser
- <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>
- package com.zq.ssm.controller;
-
- import org.apache.shiro.authz.annotation.Logical;
- import org.apache.shiro.authz.annotation.RequiresPermissions;
- import org.apache.shiro.authz.annotation.RequiresRoles;
- import org.apache.shiro.authz.annotation.RequiresUser;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @RequestMapping("/shiro")
- @Controller
- public class ShiroController {
- // RequiresUser代表 当前方法只有登录后才能够访问
- // RequiresUser等价于spring-shiro.xml中的/user/updatePwd.jsp=authc配置
- @RequiresUser
- @RequestMapping("/passUser")
- public String passUser() {
- System.out.println("身份认证通过!");
- return "admin/addUser";
- }
-
- //RequiresRoles 代表当前方法只有具备指定的角色才能访问
- // RequiresRoles等价于spring-shiro.xml中的/admin/*.jsp=roles[4]配置
- @RequiresRoles(value = {"1", "4"}, logical = Logical.AND)
- @RequestMapping("/passRole")
- public String passRole() {
- System.out.println("角色认证通过!");
- return "admin/addUser";
- }
-
- //RequiresRoles 代表当前方法只有具备指定的权限才能访问
- // RequiresRoles等价于spring-shiro.xml中的/user/teacher.jsp=perms[2]配置
- @RequiresPermissions(value = {"2"}, logical = Logical.AND)
- @RequestMapping("/passPermission")
- public String passPermission() {
- System.out.println("权限认证通过!");
- return "admin/addUser";
- }
- }
@RequiresRoles(value = {"1", "4"}, logical = Logical.AND)代表必须同时满足角色1和角色4同时认证才能通过;
@RequiresRoles(value = {"1", "4"}, logical = Logical.OR)代表必须满足角色1或者角色4任意一方认证就能通过;
报错:
报错:
通过: