目录
UserMapper.xml
- <select id="queryByroleid" resultType="java.lang.String" parameterType="java.lang.String">
- select roleid FROM t_shiro_user us,t_shiro_user_role usr
- where us.userid = usr.userid and us.username = #{userName}
- select>
-
- <select id="queryByperid" 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 rp.roleid = ur.roleid and u.userid = ur.userid and u.username = #{userName}
- select>
UserMapper
- Set
queryByroleid(@Param("userName") String userName); -
- Set
queryByperid(@Param("userName") String userName);
重写 MyRealm 中的 AuthorizationInfo 方法
- @Override
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
- String userName = principals.getPrimaryPrincipal().toString();
- Set
roleid = userBiz.queryByroleid(userName); - Set
perid = userBiz.queryByperid(userName); - SimpleAuthorizationInfo info =new SimpleAuthorizationInfo();
- info.setStringPermissions(perid);
- info.setRoles(roleid);
- 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
Springmvc.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>
ShiroController
- package com.zsx.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;
-
- /**
- * @author zsx
- * @site 155954····
- * @company 交换余生
- * @create 2022--08--26 20:25
- */
- @RequestMapping("/shiro")
- @Controller
- public class ShiroController {
- //RequiresUser 代表当前方法只有登录后才能够访问
- //RequiresUser 等价于 Spring-shiro 中的/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";
- }
-
- //RequiresPermissions 当前方法只有具备指定的权限才能访问
- //RequiresPermissions 等价于 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";
- }
-
- }