详解Java springboot 整合Shiro框架
作者:若无其事~ 时间:2022-12-14 15:56:21
Shiro介绍
Shiro是一款安全框架,主要的三个类Subject、SecurityManager、Realm
Subject:表示当前用户
SecurityManager:安全管理器,即所有与安全有关的操作都会与SecurityManager交互;且其管理着所有Subject;可以看出它是Shiro的核心,它负责与Shiro的其他组件进行交互,它相当于SpringMVC中DispatcherServlet的角色
Realm:Shiro从Realm 获取安全数据(如用户、角色、权限)
Shiro框架结构图
Springboot整合Shiro
建项目是勾选spring web,导入依赖
<!-- thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- shiro-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.7.1</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.2</version>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- thymeleaf、shiro整合包-->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
编写页面及其控制层
转发的设置,全部编写在MVCConfig中的前端控制器中
@Configurationpublic class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.addViewController("/login.html").setViewName("login"); registry.addViewController("/user/add").setViewName("user/add"); registry.addViewController("/user/update").setViewName("user/update"); registry.addViewController("/loginout").setViewName("login"); }}
连接数据库
编写application.yml
spring:
datasource:
username: ***
password: ***
url: jdbc:mysql://localhost:3306/db_2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
type-aliases-package: com.example.demo.pojo
编写 pojo、dao、service三层,dao层可以直接使Mybatis的注解。
需要的方法就是findByName(String username),通过表单传入的username值进行查询。
编写UserRealm 需要继承AuthorizingRealm
public class UserRealm extends AuthorizingRealm {
@Autowired
private IuserService iuserService;
// 授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("===>授权");
SimpleAuthorizationInfo Info = new SimpleAuthorizationInfo();
// 获取登录对象
Subject subject = SecurityUtils.getSubject();
user principal = (user) subject.getPrincipal();//拿到user
Info.addStringPermission(principal.getPerms());
return Info;
}
// 认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("==>认证");
UsernamePasswordToken authenticationToken1 = (UsernamePasswordToken) authenticationToken;
user byName = iuserService.findByName(authenticationToken1.getUsername());
if(byName==null){
return null;//抛出用户名错误的异常
}
//密码认证shiro自己完成 将user对象 传递给上面的方法进行授权
return new SimpleAuthenticationInfo(byName,byName.getPassword(),"");
}
}
代码的分析:
认证部分:
将表单提交的数据封装成一个对象,通过username从数据库中查询返回一个对象,进行比对
最后将这个查询的对象传递给授权方法。
授权部分:
获取到用户对象,给用户对象进行相应的授权。(传递的user对象中就有权限设置)
编写ShiroConfig
@Configuration
public class ShiroConfig {
@Bean //创建对象
public UserRealm userRealm(){
return new UserRealm();
}
@Bean //接管对象 @Bean 默认使用方法名称
public DefaultWebSecurityManager securityManager(@Qualifier("userRealm") Realm realm){
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
defaultWebSecurityManager.setRealm(realm);
return defaultWebSecurityManager;
}
@Bean //交给前端处理
public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
HashMap<String, String> hashMap = new HashMap<>();
// 该路径 必须通过认证 才能进行访问
hashMap.put("/user/*","authc");
// 进行授权
hashMap.put("/user/add","perms[add]");
hashMap.put("/user/update","perms[update]");
// 注销
hashMap.put("/logout","logout");
shiroFilterFactoryBean.setFilterChainDefinitionMap(hashMap);
// 设置登录页面的路径
shiroFilterFactoryBean.setLoginUrl("/login.html");
// 设置授权页面
shiroFilterFactoryBean.setUnauthorizedUrl("/noLogin");
return shiroFilterFactoryBean;
}
// 完成整合
@Bean
public ShiroDialect getShiroDialect(){
return new ShiroDialect();
}
}
代码分析
在这个配置类中,配置的方法就是ioc注入。
ShiroFilterFactoryBean中可以配置
资源路径对应的权限
登陆页面
权限不足 无法访问的页面路径
注销
补充: 拦截的属性
anon: 无需认证就可以访问
authc: 必须认证了才能访问
user: 必须拥有记住我功能才能用
perms: 拥有对某个资源的权限才能访问
role: 拥有某个角色权限
编写控制层代码
@Controller
public class logincontroller {
// 执行流程 前端表单-》控制层代码-》config
@PostMapping("/login")
public String login(String username, String password, Model model){
// 获取一个用户
Subject subject = SecurityUtils.getSubject();
// 封装用户登陆数据
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
// 执行登录方法,如果失败就会抛出异常
try{
subject.login(usernamePasswordToken);
return "index";
}catch (UnknownAccountException e){
model.addAttribute("msg","用户名错误");
return "login";
}catch (IncorrectCredentialsException e){
model.addAttribute("msg","密码错误");
return "login";
}
}
@GetMapping("/noLogin")
@ResponseBody
public String nologin(){return "未经授权 无法访问";}
}
代码分析:
login方法:获取从表单传递的数据,封装从UsernamePasswordToken对象,调用login方法进行登录操作
Shiro整合Thymeleaf
在ShiroConfig需要整合ShiroDialect
// 完成整合
@Bean
public ShiroDialect getShiroDialect(){
return new ShiroDialect();
}
约束
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
使用方法
shiro:notAuthenticated
:没有进行登录 显示shiro:authenticated:
已经登陆 显示shiro:hasPermission="A"
用户存在A的权限则显示
示例代码:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<div shiro:notAuthenticated>
<a th:href="@{/login.html}">登录</a>
</div>
<div shiro:authenticated>
<a th:href="@{/logout}">注销</a>
</div>
<div shiro:hasPermission="add">
<a th:href="@{/user/add}">ADD</a>
</div>
<div shiro:hasPermission="update">
<a th:href="@{/user/update}">UPDATE</a>
</div>
</body>
</html>
来源:https://blog.csdn.net/qq_51476114/article/details/122334002
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Java的Hibernate框架中的List映射表与Bag映射
java实现http的Post、Get、代理访问请求
c++换行符知识点总结
java swing实现简单的五子棋游戏
![](https://img.aspxhome.com/file/2023/4/83124_0s.jpg)
C# Winform实现石头剪刀布游戏
JVM内存参数配置详解
![](https://img.aspxhome.com/file/2023/7/74987_0s.png)
JavaApi实现更新删除及读取节点
![](https://img.aspxhome.com/file/2023/7/59037_0s.png)
Android 编程下字库的使用及注意事项
![](https://img.aspxhome.com/file/2023/5/139065_0s.png)
Android编程之播放器MediaPlayer实现均衡器效果示例
Android语音声波控件 Android条形波控件
Java基础学习之集合底层原理
![](https://img.aspxhome.com/file/2023/0/86820_0s.png)
Android实现授权访问网页的方法
JAVA遍历map的几种实现方法代码
浅谈java异常链与异常丢失
Android NDK开发简单程序分享(Hello Word!)
![](https://img.aspxhome.com/file/2023/0/138640_0s.png)
Java本地缓存工具之LoadingCache的使用详解
SpringBoot注册Servlet的三种方法详解
![](https://img.aspxhome.com/file/2023/1/78921_0s.png)
C# using三种使用方法
Java使用Collections.sort对中文进行排序方式
![](https://img.aspxhome.com/file/2023/1/113491_0s.png)
springcloud使用Hystrix进行微服务降级管理
![](https://img.aspxhome.com/file/2023/7/88557_0s.png)