企业中老项目还在使用ssm框架。
(1)创建一个maven的web工程。
(2)ssm整合到web工程
pom依赖
<dependencies> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>5.2.15.RELEASEversion> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.5.6version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>2.0.6version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>8.0.28version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>1.2.1version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.18.24version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.13.2.2version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>javax.servlet-apiartifactId> <version>4.0.1version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>5.2.15.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-txartifactId> <version>5.2.15.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aspectsartifactId> <version>5.2.15.RELEASEversion> dependency> <dependency> <groupId>org.mybatis.generatorgroupId> <artifactId>mybatis-generator-coreartifactId> <version>1.4.0version> dependency> <dependency> <groupId>com.github.pagehelpergroupId> <artifactId>pagehelperartifactId> <version>5.3.0version> dependency> <dependency> <groupId>log4jgroupId> <artifactId>log4jartifactId> <version>1.2.17version> dependency> dependencies>spring配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.wzh"/> <mvc:annotation-driven /> <mvc:default-servlet-handler/> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" /> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager" /> bean> <bean id="dataResource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/shiro?serverTimezone=Asia/Shanghai"/> <property name="username" value="root"/> <property name="password" value="123456"/> <property name="initialSize" value="10"/> <property name="minIdle" value="5"/> <property name="maxActive" value="10"/> <property name="maxWait" value="2000"/> bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataResource"/> bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataResource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <property name="plugins" > <array> <bean class="com.github.pagehelper.PageInterceptor"/> array> property> bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wzh.mapper"/> bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="realm"/> bean> <bean id="realm" class="com.wzh.realm.MyRealm"> <property name="credentialsMatcher" ref="credentialsMatcher"/> bean> <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="MD5"/> <property name="hashIterations" value="1024"/> bean> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="filterChainDefinitions"> <value> /login=anon /**=authc value> property> <property name="filters"> <map> <entry key="authc"> <bean class="com.wzh.filter.LoginFilter"/> entry> map> property> bean> beans>web.xml配置文件
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <filter> <filter-name>shiroFilterfilter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class> filter> <filter-mapping> <filter-name>shiroFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> <servlet> <servlet-name>DispatcherServletservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:springmvc.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>DispatcherServletservlet-name> <url-pattern>/url-pattern> servlet-mapping> <filter> <filter-name>encodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>utf-8param-value> init-param> filter> <filter-mapping> <filter-name>encodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> web-app>

数据结构
张三 -user:query user:add user:update user:delete
李四 ---》user:query user:add user:update王五-----》user:query user:export
整合shiro

(1)引入shiro的依赖
-
- <dependency>
- <groupId>org.apache.shirogroupId>
- <artifactId>shiro-springartifactId>
- <version>1.9.0version>
- dependency>
(2)修改spring配置文件
-
-
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="realm"/>
- bean>
-
- <bean id="realm" class="com.ykq.realm.MyRealm">
- <property name="credentialsMatcher" ref="credentialsMatcher"/>
- bean>
-
-
- <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
- <property name="hashAlgorithmName" value="MD5"/>
- <property name="hashIterations" value="1024"/>
- bean>
-
-
- <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
-
- <property name="loginUrl" value="/login.jsp"/>
-
- <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
-
- <property name="filterChainDefinitions">
- <value>
- /login=anon
- /**=authc
- value>
- property>
- bean>
shiro中内置很多过滤器,而每个过滤都有相应的别名.

(3) 修改web.xml文件
-
- <filter>
- <filter-name>shiroFilterfilter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
- filter>
- <filter-mapping>
- <filter-name>shiroFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- <%--
- Created by IntelliJ IDEA.
- User: m1762
- Date: 2022/8/4
- Time: 22:58
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <shiro:hasPermission name="user:query">
- <a href="/query">查询用户a>
- shiro:hasPermission>
- <shiro:hasPermission name="user:add">
- <a href="/add">添加用户a>
- shiro:hasPermission>
- <shiro:hasPermission name="user:delete">
- <a href="/delete">删除用户a>
- shiro:hasPermission>
- <shiro:hasPermission name="user:update">
- <a href="/update">修改用户a>
- shiro:hasPermission>
- <shiro:hasPermission name="user:export">
- <a href="/export">导出用户a>
- shiro:hasPermission>
- body>
- html>

可以在jsp中获取当前登录者的账号
<h1>欢迎<shiro:principal property="username"/>来到主页h1>

上面只是在网页中根据不同用户显示不同的菜单,这种方式只能防君子不能防小人。因为现在依旧可以通过postman访问没有的权限方法 比如张三可以访问到user:export路径
解决办法:
拦截器---获取请求路径 然后根据你的路径判断当前用户是否具有该权限。
spring整合shiro时提供了一个注解:可以加载相应方法上。
使用注解:
1.springmvc.xml中启动shiro的注解
-
- <bean id="lifecycleBeanPostProcessor"
- class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
- depends-on="lifecycleBeanPostProcessor" />
- <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
- <property name="securityManager" ref="securityManager" />
- bean>

(2)使用注解


这个太丑了,我们想要跳转一个页面,
我们之前学过全局异常处理: 没有登录会报这个异常

所谓前后端完全分离:后端响应的都是json数据,而不再是网页。
我们需要修改的就是:
1. 登录成功或者失败应该返回json数据
2. 当未登录时返回的也是json数据
3. 访问未授权的资源,也要分会json。
修改登录接口

(1)创建一个过滤器,继承登录校验的FormAuthenticationFilter接口。
- package com.wzh.filter;
-
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.wzh.utils.CommonResult;
- import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
-
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import java.io.PrintWriter;
-
- /**
- * @ProjectName: shiro-ssm0805
- * @Package: com.wzh.filter
- * @ClassName: LoginFilter
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/5 16:58
- * @Version: 1.0
- */
- public class LoginFilter extends FormAuthenticationFilter {
- /**
- * 当没有登录时会经过该方法,如果想让它返回json数据必须重写onAccessDenied这个方法
- * @param request
- * @param response
- * @return
- * @throws Exception
- */
- @Override
- protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
- response.setContentType("application/json;charset=utf-8");
- PrintWriter writer = response.getWriter();
- CommonResult commonResult = CommonResult.UNLOGIN;
- //jackson中内置对象 将java对象转为json对象
- ObjectMapper objectMapper = new ObjectMapper();
- String json = objectMapper.writeValueAsString(commonResult);
- //响应给客户json数据
- writer.print(json);
- writer.flush();
- writer.close();
- return false;
- }
- }
之前默认是在springmvc.xml中配置的跳转页面

(2) 注册我们的过滤器


项目结构:

Controller层:
UserController: 用于登录 调用login方法判断是否身份认证 授权
- package com.wzh.controller;
-
- import com.wzh.utils.CommonResult;
- import org.apache.shiro.SecurityUtils;
- import org.apache.shiro.authc.UsernamePasswordToken;
- import org.apache.shiro.subject.Subject;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.controller
- * @ClassName: UserController
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:07
- * @Version: 1.0
- */
- @RestController
- public class UserController {
- @RequestMapping("login")
- public CommonResult login(String username,String password){
- System.out.println(username);
- //获取subject主体对象
- Subject subject = SecurityUtils.getSubject();
- UsernamePasswordToken token = new UsernamePasswordToken(username, password);
- try{
- subject.login(token);
- System.out.println("是否身份认证:"+subject.isAuthenticated());
- System.out.println("是否授权:"+subject.isPermitted("查询"));
- System.out.println("是否授权:"+subject.isPermitted("添加"));
- System.out.println("是否授权:"+subject.isPermitted("修改"));
- System.out.println("是否授权:"+subject.isPermitted("删除"));
- System.out.println("是否授权:"+subject.isPermitted("导出"));
- System.out.println("是否授权:"+subject.hasRole("超级管理员"));
- System.out.println("是否授权:"+subject.hasRole("管理员"));
- System.out.println("是否授权:"+subject.hasRole("用户"));
-
-
- return CommonResult.LOGIN_SUCCESS;
- }catch (Exception e){
- e.printStackTrace();
- return CommonResult.LOGIN_ERROR;
- }
- }
- }
PermissionController: 用于登录之后查看是否授权
- package com.wzh.controller;
-
- import com.wzh.utils.CommonResult;
- import org.apache.shiro.authz.annotation.Logical;
- import org.apache.shiro.authz.annotation.RequiresPermissions;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @ProjectName: shiro-ssm0805
- * @Package: com.wzh.controller
- * @ClassName: PermissionController
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/5 9:57
- * @Version: 1.0
- */
- @RestController
- public class PermissionController {
-
- @GetMapping("/query")
- //使用shiro注解
- @RequiresPermissions(value = {"user:query","user:aaa"},logical = Logical.OR)
- public String query(){
- return "query";
- }
- @RequestMapping("/add")
- @RequiresPermissions(value = {"user:add"})
- public String add(){
- return "add";
- }
- @RequestMapping("/delete")
- @RequiresPermissions(value = {"user:delete"})
- public String delete(){
- return "delete";
- }
- @RequestMapping("/update")
- @RequiresPermissions(value = {"user:update"})
- public String update(){
- return "update";
- }
- @RequestMapping("/export")
- @RequiresPermissions(value = {"user:export"})
- public String export(){
- return "export";
- }
- }
service层:
UserService:
- package com.wzh.service;
-
- import com.wzh.entity.User;
-
- import java.util.List;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.service
- * @ClassName: UserService
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:19
- * @Version: 1.0
- */
- public interface UserService {
- User findByUsername(String username);
-
- }
PermissionService:
- package com.wzh.service;
-
- import java.util.List;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.service
- * @ClassName: PermissionService
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:27
- * @Version: 1.0
- */
- public interface PermissionService {
-
-
- List
findPermissionById(Integer userid); - }
RoleService:
- package com.wzh.service;
-
- import java.util.List;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.service
- * @ClassName: RoleService
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:27
- * @Version: 1.0
- */
- public interface RoleService {
-
- List
findRolesById(Integer userid); - }
UserServiceImpl:
- package com.wzh.service.impl;
-
- import com.wzh.entity.User;
- import com.wzh.mapper.UserMapper;
- import com.wzh.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.service.impl
- * @ClassName: UserServiceImpl
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:20
- * @Version: 1.0
- */
- @Service
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserMapper userMapper;
-
- @Override
- public User findByUsername(String username) {
- if(username!=null&&username!="") {
- User user = userMapper.selectByUsername(username);
- return user;
- }
- return null;
- }
-
-
- }
PermissionServiceImpl:
- package com.wzh.service.impl;
-
- import com.wzh.mapper.PermissionMapper;
- import com.wzh.service.PermissionService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.service.impl
- * @ClassName: PermissionServiceImpl
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:28
- * @Version: 1.0
- */
- @Service
- public class PermissionServiceImpl implements PermissionService {
- @Autowired
- private PermissionMapper permissionMapper;
-
-
- @Override
- public List
findPermissionById(Integer userid) { - List
list = permissionMapper.selectByUserId(userid); - return list;
- }
- }
RoleServiceImpl:
- package com.wzh.service.impl;
-
- import com.wzh.mapper.RoleMapper;
- import com.wzh.service.RoleService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.service.impl
- * @ClassName: RoleServiceImpl
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:28
- * @Version: 1.0
- */
- @Service
- public class RoleServiceImpl implements RoleService {
- @Autowired
- private RoleMapper roleMapper;
- @Override
- public List
findRolesById(Integer userid) { - List
list = roleMapper.selectByUserId(userid); - return list;
- }
- }
mapper层:
UserMapper:
- package com.wzh.mapper;
-
- import com.wzh.entity.User;
-
- import java.util.List;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.mapper
- * @ClassName: UserMapper
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:21
- * @Version: 1.0
- */
- public interface UserMapper {
- User selectByUsername(String username);
-
-
- }
PermissionMapper:
- package com.wzh.mapper;
-
- import java.util.List;
-
- /**
- @ProjectName: ssm-shiro
- @Package: com.wzh.mapper
- @ClassName: PermissionMapper
- @Author: 王振华
- @Description:
- @Date: 2022/8/4 22:21
- @Version: 1.0
- */
- public interface PermissionMapper {
- List
selectByUserId(Integer userid); - }
RoleMapper:
- package com.wzh.mapper;
-
- import java.util.List;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.mapper
- * @ClassName: RoleMapper
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:21
- * @Version: 1.0
- */
-
- public interface RoleMapper {
- List
selectByUserId(Integer userid); - }
entity层:
User:
- package com.wzh.entity;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import javax.management.relation.Role;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.entity
- * @ClassName: User
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:19
- * @Version: 1.0
- */
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- public class User {
- private Integer userid;
-
- private String username;
-
- private String userpwd;
-
- private String sex;
-
- private String address;
-
- private String salt;
- private Permission permission;
- private Role role;
- }
Permission:
- package com.wzh.entity;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.entity
- * @ClassName: Permission
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:22
- * @Version: 1.0
- */
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- public class Permission {
- private Integer perid;
-
- private String pername;
-
- private String percode;
- }
Role:
- package com.wzh.entity;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- /**
- * @ProjectName: ssm-shiro
- * @Package: com.wzh.entity
- * @ClassName: Role
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 22:22
- * @Version: 1.0
- */
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- public class Role {
- private Integer roleid;
-
- private String rolename;
- }
filter:
LoginFilter: 用于未登录返回json数据
- package com.wzh.filter;
-
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.wzh.utils.CommonResult;
- import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
-
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import java.io.PrintWriter;
-
- /**
- * @ProjectName: shiro-ssm0805
- * @Package: com.wzh.filter
- * @ClassName: LoginFilter
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/5 16:58
- * @Version: 1.0
- */
- public class LoginFilter extends FormAuthenticationFilter {
- /**
- * 当没有登录时会经过该方法,如果想让它返回json数据必须重写onAccessDenied这个方法
- * @param request
- * @param response
- * @return
- * @throws Exception
- */
- @Override
- protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
- response.setContentType("application/json;charset=utf-8");
- PrintWriter writer = response.getWriter();
- CommonResult commonResult = CommonResult.UNLOGIN;
- //jackson中内置对象 将java对象转为json对象
- ObjectMapper objectMapper = new ObjectMapper();
- String json = objectMapper.writeValueAsString(commonResult);
- //响应给客户json数据
- writer.print(json);
- writer.flush();
- writer.close();
- return false;
- }
- }
handler: 全局异常处理类 用户没有权限返回json数据给前端
MyException:
- package com.wzh.handler;
-
- import com.wzh.utils.CommonResult;
- import org.apache.shiro.authz.UnauthorizedException;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- /**
- * @ProjectName: shiro-ssm0805
- * @Package: com.wzh.handler
- * @ClassName: MyException
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/5 16:42
- * @Version: 1.0
- */
- @ControllerAdvice //异常处理类
- public class MyException {
- //当发生该异常时触发该方法
- @ExceptionHandler(value = UnauthorizedException.class)
- @ResponseBody
- public CommonResult Unauth(UnauthorizedException e){
- e.printStackTrace();
- return CommonResult.UNAUTHORIZED;
- }
- }
realm: 自定义的认证授权规则
MyRealm:
- package com.wzh.realm;
-
-
- import com.wzh.entity.User;
- import com.wzh.service.PermissionService;
- import com.wzh.service.RoleService;
- import com.wzh.service.UserService;
- 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 org.springframework.beans.factory.annotation.Autowired;
-
- import java.util.List;
-
- /**
- * @ProjectName: shiro
- * @Package: com.wzh.demo02
- * @ClassName: MyRealm
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/4 19:44
- * @Version: 1.0
- */
- public class MyRealm extends AuthorizingRealm {
- @Autowired
- private UserService userService;
-
- @Autowired
- private PermissionService permissionService;
-
- @Autowired
- private RoleService roleService;
-
- @Override
- //该方法用于完成认证的功能
- protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
- //1.根据token获取账号
- String username = (String) authenticationToken.getPrincipal();
- /**
- * 以前登陆的逻辑是 把用户和密码全部发到数据库 去匹配
- * 在shrio里面是先根据用户名把用户对象查询出来,再来做密码匹配
- */
-
- //2.根据账号查询用户信息
- User user = userService.findByUsername(username);
- //表示该用户名在数据库中存在
- if(user!=null){
- /**
- * 参数说明
- * 参数1:可以传到任意对象
- * 参数2:从数据库里面查询出来的密码
- * 参数3:盐
- * 参数4:当前类名
- */
- ByteSource credentialsSalt = ByteSource.Util.bytes(user.getSalt());
- SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getUserpwd(),credentialsSalt,this.getName());
- return info;
- }
- //用户不存在 shiro会抛 UnknowAccountException
- return null;
- }
-
-
-
- //授权
- @Override
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
- User user = (User) principalCollection.getPrimaryPrincipal();
- SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
- //根据账号查找该用户具有哪些权限
- List
list = permissionService.findPermissionById(user.getUserid()); - if(list!=null&&list.size()>0){
- info.addStringPermissions(list);
- }
- List
roles = roleService.findRolesById(user.getUserid()); - if(roles!=null&&roles.size()>0){
- info.addRoles(roles);
- }
- return info;
-
- }
-
-
- }
util:
CommonResult:
- package com.wzh.utils;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- /**
- * @ProjectName: shiro-ssm0805
- * @Package: com.wzh.utils
- * @ClassName: CommonResult
- * @Author: 王振华
- * @Description:
- * @Date: 2022/8/5 11:02
- * @Version: 1.0
- */
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- public class CommonResult {
- public static final CommonResult UNLOGIN = new CommonResult(403,"未登录",null);
- public static final CommonResult UNAUTHORIZED = new CommonResult(405,"未授权",null);
- public static final CommonResult LOGIN_SUCCESS = new CommonResult(200,"登录成功",null);
- public static final CommonResult LOGIN_ERROR = new CommonResult(-1,"登录失败",null);
- private Integer code;
-
- private String msg;
-
- private Object data;
-
- }
