当前位置: 首页 > news >正文

8 shiro的web整合

一、shiro的web流程

在这里插入图片描述

二、Shiro整合SSM

新建一个maven项目ssm,pom如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.simon</groupId><artifactId>shiro-web-ssm</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.7</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><scope>provided</scope><version>3.1.0</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.4.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.8</version></dependency></dependencies><!--<build><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>8080</port><path>/</path><uriEncoding>UTF-8</uriEncoding></configuration></plugin></plugins></build>-->
</project>

项目整体结构如下:
在这里插入图片描述

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"><!--    配置Shiro整合web的过滤器--><filter><!--        默认情况下,请求到达这个过滤器,会去Spring容器中名字为filter-name的实例去处理--><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext-*.xml</param-value></context-param></web-app>
applicationContext-mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"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"><context:property-placeholder location="classpath:db.properties" /><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><bean class="org.mybatis.spring.SqlSessionFactoryBean" ><property name="dataSource" ref="dataSource" /><property name="mapperLocations" value="classpath:mapper/*.xml" /></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.simon.mapper" /></bean></beans>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"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"><context:component-scan base-package="com.simon.service" /></beans>
applicationContext-shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--    构建realm--><bean id="realm" class="com.simon.realm.ShiroRealm" /><!--    构建securityManager--><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="realm"/></bean><!--    构建ShiroFilter实例--><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><property name="securityManager" ref="securityManager"/><property name="loginUrl" value="/login.html" /><property name="filterChainDefinitionMap"><map><entry key="/login.html" value="anon" /><entry key="/user/**" value="anon" /><entry key="/**" value="authc" /></map></property></bean>
</beans>
db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///shiro-web
jdbc.username=root
jdbc.password=root
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<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: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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><mvc:annotation-driven /><context:component-scan base-package="com.simon.controller" /><mvc:default-servlet-handler /></beans>
表结构
/*Navicat Premium Data TransferSource Server         : shiroSource Server Type    : MySQLSource Server Version : 80027Source Host           : localhost:3306Source Schema         : shiro-webTarget Server Type    : MySQLTarget Server Version : 80027File Encoding         : 65001Date: 23/09/2025 18:01:14
*/SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for role_perm
-- ----------------------------
DROP TABLE IF EXISTS `role_perm`;
CREATE TABLE `role_perm`  (`rid` int(0) NULL DEFAULT NULL,`pid` int(0) NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of role_perm
-- ----------------------------
INSERT INTO `role_perm` VALUES (1, 2);
INSERT INTO `role_perm` VALUES (1, 2);
INSERT INTO `role_perm` VALUES (1, 3);
INSERT INTO `role_perm` VALUES (1, 4);
INSERT INTO `role_perm` VALUES (2, 4);
INSERT INTO `role_perm` VALUES (2, 4);
INSERT INTO `role_perm` VALUES (2, 4);
INSERT INTO `role_perm` VALUES (3, 4);-- ----------------------------
-- Table structure for tb_permission
-- ----------------------------
DROP TABLE IF EXISTS `tb_permission`;
CREATE TABLE `tb_permission`  (`id` int(0) NOT NULL AUTO_INCREMENT,`perm_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of tb_permission
-- ----------------------------
INSERT INTO `tb_permission` VALUES (1, 'user:add');
INSERT INTO `tb_permission` VALUES (2, 'user:update');
INSERT INTO `tb_permission` VALUES (3, 'user:delete');
INSERT INTO `tb_permission` VALUES (4, 'user:select');-- ----------------------------
-- Table structure for tb_role
-- ----------------------------
DROP TABLE IF EXISTS `tb_role`;
CREATE TABLE `tb_role`  (`id` int(0) NOT NULL AUTO_INCREMENT,`role_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of tb_role
-- ----------------------------
INSERT INTO `tb_role` VALUES (1, '超级管理员');
INSERT INTO `tb_role` VALUES (2, '运营');
INSERT INTO `tb_role` VALUES (3, '市场');-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user`  (`id` int(0) NOT NULL AUTO_INCREMENT,`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,`salt` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES (1, 'admin', 'admin', 'admin');
INSERT INTO `tb_user` VALUES (2, 'root', 'root', 'root');-- ----------------------------
-- Table structure for user_role
-- ----------------------------
DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role`  (`uid` int(0) NULL DEFAULT NULL,`rid` int(0) NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of user_role
-- ----------------------------
INSERT INTO `user_role` VALUES (1, 1);
INSERT INTO `user_role` VALUES (1, 2);
INSERT INTO `user_role` VALUES (1, 3);
INSERT INTO `user_role` VALUES (2, 2);
INSERT INTO `user_role` VALUES (2, 3);SET FOREIGN_KEY_CHECKS = 1;
实体类
package com.simon.entity;/***/
public class Permission {private Integer id;private String permName;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getPermName() {return permName;}public void setPermName(String permName) {this.permName = permName;}
}
package com.simon.entity;/***/
public class Role {private Integer id;private String roleName;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName = roleName;}
}
package com.simon.entity;/*** 映射用户表*/
public class User {private Integer id;private String username;private String password;private String salt;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getSalt() {return salt;}public void setSalt(String salt) {this.salt = salt;}
}
service
package com.simon.service;import com.simon.entity.User;public interface UserService {User findByUsername(String username);}
package com.simon.service;import java.util.Set;import com.simon.entity.Role;public interface RoleService {/*** 根据用户id查询角色信息* @param uid* @return*/Set<Role> findRolesByUid(Integer uid);}
package com.simon.service;import java.util.Set;
import com.simon.entity.Permission;public interface PermissionService {/*** 根据角色id,查询角色对应的权限* @param roleIdSet* @return*/Set<Permission> findPermsByRoleSet(Set<Integer> roleIdSet);
}
package com.simon.service.impl;import com.simon.entity.User;
import com.simon.mapper.UserMapper;
import com.simon.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic User findByUsername(String username) {return userMapper.findUserByUsername(username);}}
package com.simon.service.impl;import com.simon.entity.Role;
import com.simon.mapper.RoleMapper;
import com.simon.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Set;@Service
public class RoleServiceImpl implements RoleService {@Autowiredprivate RoleMapper roleMapper;@Overridepublic Set<Role> findRolesByUid(Integer uid) {return roleMapper.findRolesByUid(uid);}
}
package com.simon.service.impl;import com.simon.entity.Permission;
import com.simon.mapper.PermissionMapper;
import com.simon.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Set;@Service
public class PermissionServiceImpl implements PermissionService {@Autowiredprivate PermissionMapper permissionMapper;@Overridepublic Set<Permission> findPermsByRoleSet(Set<Integer> roleIdSet) {return permissionMapper.findPermsByRoleIdIn(roleIdSet);}
}
mapper
package com.simon.mapper;import com.simon.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;public interface UserMapper {@Select("select * from tb_user where username = #{username}")User findUserByUsername(@Param("username")String username);}
package com.simon.mapper;import com.simon.entity.Role;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;import java.util.Set;public interface RoleMapper {@Select("select r.* from tb_role r, user_role ur where r.id = ur.rid and ur.uid = #{uid}")Set<Role> findRolesByUid(@Param("uid")Integer uid);}
package com.simon.mapper;import com.simon.entity.Permission;
import org.apache.ibatis.annotations.Param;import java.util.Set;public interface PermissionMapper {Set<Permission> findPermsByRoleIdIn(@Param("roleIdSet") Set<Integer> roleIdSet);
}
ShiroRealm
package com.simon.realm;import com.alibaba.druid.util.StringUtils;
import com.simon.entity.Permission;
import com.simon.entity.Role;
import com.simon.entity.User;
import com.simon.service.PermissionService;
import com.simon.service.RoleService;
import com.simon.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.authc.credential.HashedCredentialsMatcher;
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 org.springframework.stereotype.Component;import java.util.HashSet;
import java.util.Set;public class ShiroRealm extends AuthorizingRealm {@Autowiredprivate UserService userService;@Autowiredprivate RoleService roleService;@Autowiredprivate PermissionService permissionService;{HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();matcher.setHashAlgorithmName("MD5");matcher.setHashIterations(1024);this.setCredentialsMatcher(matcher);}@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {//1. 基于Token获取用户名String username = (String) token.getPrincipal();//2. 判断用户名(非空)if(StringUtils.isEmpty(username)){// 返回null,会默认抛出一个异常,org.apache.shiro.authc.UnknownAccountExceptionreturn null;}//3. 如果用户名不为null,基于用户名查询用户信息User user = userService.findByUsername(username);//4. 判断user对象是否为nullif(user == null){return null;}//5. 声明AuthenticationInfo对象,并填充用户信息SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user,user.getPassword(),"ShiroRealm!!");// 设置盐!info.setCredentialsSalt(ByteSource.Util.bytes(user.getSalt()));//6. 返回inforeturn info;}@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {//1. 获取认证用户的信息User user = (User) principals.getPrimaryPrincipal();//2. 基于用户信息获取当前用户拥有的角色。Set<Role> roleSet = roleService.findRolesByUid(user.getId());Set<Integer> roleIdSet = new HashSet<>();Set<String> roleNameSet = new HashSet<>();for (Role role : roleSet) {roleIdSet.add(role.getId());roleNameSet.add(role.getRoleName());}//3. 基于用户拥有的角色查询权限信息Set<Permission> permSet = permissionService.findPermsByRoleSet(roleIdSet);Set<String> permNameSet = new HashSet<>();for (Permission permission : permSet) {permNameSet.add(permission.getPermName());}//4. 声明AuthorizationInfo对象作为返回值,传入角色信息和权限信息SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();info.setRoles(roleNameSet);info.setStringPermissions(permNameSet);//5. 返回return info;}
}
controller
package com.simon.controller;import com.simon.entity.User;
import com.simon.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/test")public User test(String username){return userService.findByUsername(username);}}
配置tomcat,启动服务

注意: 可以通过maven插件或者本地server的方式启动

http://www.dtcms.com/a/398282.html

相关文章:

  • iOS 26 系统电耗分析实战指南 如何检测电池掉电、液体玻璃导致的能耗变化
  • 自动化平台自动化能力统一的建设
  • 做网站学的是代码吗网站备案流程教程
  • 【Unity 入门教程】二、核心概念
  • 【春秋云镜】CVE-2022-30887(文件上传/rce)
  • [iOS] YYModel 初步学习
  • 视频录屏软件 视频录屏软件 Bandicam (班迪录屏) 8.2.2.2531
  • 今天继续学习nginx服务部署与配置
  • flutter 编译报错java.util.zip.ZipException: zip END header not found
  • 网站建设精英京东商城网站域名
  • 《AI工具驱动的分布式任务调度系统从0到1实践解析》
  • C#练习——事件
  • 深拷贝浅拷贝的区别?如何实现⼀个深拷贝?
  • C primer plus (第六版)第十一章 编程练习第10题
  • AgentScope Studio 安装与测试
  • 长沙房产交易中心官网做seo网站空间
  • 金融培训网站源码淘宝基地网站怎么做
  • Spark核心Storage详解
  • 高系分二十:微服务系统分析与设计
  • 深度学习----ResNet(残差网络)-彻底改变深度神经网络的训练方式:通过残差学习来解决深层网络退化问题(附PyTorch实现)
  • 脑电模型实战系列:入门脑电情绪识别-用最简单的DNN模型起步
  • 赣州企业网站建设比较火的推广软件
  • 广州公司网站制作网页游戏排行榜20
  • 算法提升之单调数据结构-(单调队列)
  • PHP 线上环境 Composer 依赖包更新部署指南-简易版
  • 设计模式-原型模式详解
  • ESP8266与CEM5826-M11毫米波雷达传感器的动态检测系统
  • [原创]怎么用qq邮箱订阅arxiv.org?
  • 设计模式-中介者模式详解
  • 【探寻C++之旅】第十四章:简单实现set和map