目录
一,shiro授权角色,权限
分析:
1.分析用户具备哪些角色
2.分析用户具备什么权限
1.sql语句编写
--角色:用户具备哪些角色
select roleid FROM t_shiro_user u,t_shiro_user_role ur where u.userid = ur.userid and u.username = 'zdm'
--权限:用户具备的权限
SELECT rp.perid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role_permission rp
where u.userid = ur.userid and ur.roleid = rp.roleid and u.username = 'ls'
2.自动生成xml文件配置
<select id="selectRolesByUserName" resultType="java.lang.String" parameterType="java.lang.Integer">
select roleid FROM t_shiro_user u,t_shiro_user_role ur where u.userid = ur.userid and u.username = #{userName}
</select>
<select id="selectgetPersByUserName" resultType="java.lang.String" parameterType="java.lang.Integer">
SELECT rp.perid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role_permission rp
where u.userid = ur.userid and ur.roleid = rp.roleid and u.username = #{userName}
</select>
Service层编写
package com.dengxiyan.ssm.Biz;
import com.dengxiyan.ssm.model.User;
import org.apache.ibatis.annotations.Param;
import java.util.Set;
public interface UserBiz {
Set<String> selectRolesByUserName (String userName);
Set<String> selectgetPersByUserName(String userName);
}
实现类
package com.dengxiyan.ssm.Biz.impl;
import com.dengxiyan.ssm.Biz.UserBiz;
import com.dengxiyan.ssm.mapper.UserMapper;
import com.dengxiyan.ssm.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* @author dxy
* @site www.javadxy.com
* @company ds公司
* @create 2022-08-25 13:49
*/
@Service("userBiz")
public class UserBizImpl implements UserBiz {
@Override
public Set<String> selectRolesByUserName(String userName) {
return userMapper.selectRolesByUserName(userName);
}
@Override
public Set<String> selectgetPersByUserName(String userName) {
return userMapper.selectgetPersByUserName(userName);
}
}
Reaml
注意:角色与权限的结果要与spring-shiro.xml中的配置保持一致
package com.dengxiyan.ssm.shiro;
import com.dengxiyan.ssm.Biz.UserBiz;
import com.dengxiyan.ssm.mapper.UserMapper;
import com.dengxiyan.ssm.model.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import java.util.Set;
/**
* @author dxy
* @site www.javadxy.com
* @company ds公司
* @create 2022-08-25 13:56
*/
public class MyRealm extends AuthorizingRealm {
private UserBiz userBiz;
public UserBiz getUserBiz() {
return userBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
/**
* 授权
* @param principals
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
System.out.println("用户授权~~~");
String username = principals.getPrimaryPrincipal().toString();//获取用户名
Set<String> roles = userBiz.selectRolesByUserName(username);
Set<String> pers = userBiz.selectgetPersByUserName(username);
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//将当前登入的 权限 交给 shiro的授权器
info.setStringPermissions(pers);
//将当前登录的 角色 交给 shiro授权器
info.setRoles(roles);
return info;
}
}
二,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 --------(权限配置)
使用
Controller层
对应的注解添加到指定需要权限控制的方法上
package com.dengxiyan.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;
import javax.servlet.http.HttpServletRequest;
/**
* @author dxy
* @site www.javadxy.com
* @company ds公司
* @create 2022-08-26 18:58
*/
@Controller
public class ShiroController {
//表示登入后才可以访问 等价于Spring-shiro.xml中的user/updatePwd.jsp=配置
@RequiresUser
@RequestMapping("/passUser")
public String passUser(HttpServletRequest request){
return "admin/addUser";
}
// @RequiresRoles 当前方法只有 具备指定的角色才能访问
// 相当于spring-shiro.admin/*.jsp=roles[4]配置
@RequiresRoles(value = {"1","4"},logical = Logical.AND)
@RequestMapping("/passRole")
public String passRoles(HttpServletRequest request){
System.out.println("角色验证通过");
return "admin/addUser";
}
// @RequiresPermissions 当前方法只有具备指定的权限 才能够访问
// 等价于Spring-shiro.xml中的user/teacher.jsp=perms配置
// @RequiresPermissions(value = {"user:update","user:view"},logical = Logical.OR)
@RequiresPermissions(value = {"2"},logical = Logical.AND)
@RequestMapping("/passPer")
public String passPer(HttpServletRequest request){
System.out.println("权限通过验证");
return "admin/addUser";
}
@RequestMapping("/unauthorized")
public String unauthorized(){
System.out.println("权限验证通过");
return "admin/addUser";
}
}
Spring-mvc.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界面(测试)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="r" uri="http://shiro.apache.org/tags" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>主界面<%=System.currentTimeMillis()%>,欢迎您:[${sessionScope.username}]</h1>
<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>
</body>
</html>
效果图展示
标记处:由于每个用户的权限不同,有的用户有该权限,有的用户没有