SpringBoot 整合 Shiro 密码登录的实现代码
作者:风青宇 时间:2023-11-10 11:27:59
导入依赖(pom.xml)
<!--整合Shiro安全框架-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<!--集成jwt实现token认证-->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.2.0</version>
</dependency>
创建 ShiroConfig 配置类
@Configuration
public class ShiroConfig {
/**
* ShiroFilterFactoryBean
*/
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
//设置安全管理器
factoryBean.setSecurityManager(defaultWebSecurityManager);
// 添加shiro的内置过滤器
/*
* anon:无需认证就可以访问
* authc:必须认证才能访问
* user:必须拥有 记住我 功能才能用
* perms:拥有对某个资源的权限能访问
* role:拥有某个角色权限能访问
*/
Map<String, String> filterMap = new LinkedHashMap<>();
// 放行不需要权限认证的接口
//放行登录接口
filterMap.put("/login/**", "anon");
//放行用户接口
filterMap.put("/", "anon"); // 网站首页
//认证管理员接口
filterMap.put("/administrators/**", "authc");
factoryBean.setFilterChainDefinitionMap(filterMap);
// 设置无权限时跳转的 url
// 设置登录的请求
factoryBean.setLoginUrl("/login/toLogin");
return factoryBean;
}
/**
* 注入 DefaultWebSecurityManager
*/
@Bean(name = "securityManager")
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("customRealm") CustomRealm customRealm) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//关联CustomRealm
securityManager.setRealm(customRealm);
return securityManager;
}
/**
* 注入 securityManager
*/
@Bean
public CustomRealm customRealm() {
return new CustomRealm();
}
}
创建密码登录时验证授权 CustomRealm 类
@Component
public class CustomRealm extends AuthorizingRealm {
@Autowired
AdministratorsService administratorsService;
/*
* 设置加密方式
*/
{
HashedCredentialsMatcher mather = new HashedCredentialsMatcher();
// 加密方式
mather.setHashAlgorithmName("md5");
// 密码进行一次运算
mather.setHashIterations(512);
this.setCredentialsMatcher(mather);
}
/**
* 授权
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
System.out.println("————授权————doGetAuthorizationInfo————");
return null;
}
/**
* 认证
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("————认证————doGetAuthenticationInfo————");
UsernamePasswordToken userToken = (UsernamePasswordToken) token;
// 连接数据库 查询用户数据
QueryWrapper<Administrators> wrapper = new QueryWrapper<>();
wrapper.eq("username", userToken.getUsername());
Administrators administrators = administratorsService.getOne(wrapper);
if (administrators == null) {
return null; // 抛出异常 UnknownAccountException
}
// 密码认证,shiro做
return new SimpleAuthenticationInfo("", administrators.getPassword(), "");
}
}
控制层用户密码登录
//用户名登录
@ApiOperation(value = "管理员登录", notes = "用户名登录--不进行拦截")
@PostMapping("/doLogin")
public String doLogin(@RequestParam("username") String username,
@RequestParam("password") String password,
HttpSession session,Model model) {
// 获取当前的用户
Subject subject = SecurityUtils.getSubject();
// 封装用户的登录数据
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);
//保存session会话 管理员名字
session.setAttribute("adname", username);
return "admin";
} catch (UnknownAccountException e) {
model.addAttribute("usererror", "用户名错误!请重新输入。");
return "login";
} catch (IncorrectCredentialsException ice) {
model.addAttribute("pwerror", "密码错误!请重新输入。");
return "login";
}
}
来源:https://www.cnblogs.com/dmflysky/p/14451029.html
标签:SpringBoot,整合,Shiro,密码登录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C++实现LeetCode(205.同构字符串)
2023-06-21 04:06:54
QT5实现简单的TCP通信的实现
2023-11-02 21:24:48
![](https://img.aspxhome.com/file/2023/1/105171_0s.png)
线程局部变量的实现 ThreadLocal使用及场景介绍
2023-11-10 03:19:26
![](https://img.aspxhome.com/file/2023/1/59031_0s.jpg)
go打包aar及flutter调用aar流程详解
2023-06-24 17:39:21
![](https://img.aspxhome.com/file/2023/4/87634_0s.png)
Android基础之使用Fragment控制切换多个页面
2023-07-11 00:08:37
详解springboot和vue前后端分离开发跨域登陆问题
2023-08-07 00:48:38
SpringBoot中打war包需要注意事项
2023-11-05 19:44:42
![](https://img.aspxhome.com/file/2023/6/58616_0s.png)
Java 使用IO流实现大文件的分割与合并实例详解
2023-08-23 09:33:33
![](https://img.aspxhome.com/file/2023/8/58078_0s.png)
JAVA8 十大新特性详解
2023-07-02 10:03:27
Android Flutter使用本地数据库编写备忘录应用
2023-09-15 17:24:09
![](https://img.aspxhome.com/file/2023/6/109226_0s.webp)
Bootstrap 下拉菜单.dropdown的具体使用方法
2023-07-08 19:10:46
java isInterrupted()判断线程的实例讲解
2023-07-21 01:45:53
java面试题——详解HashMap和Hashtable 的区别
2023-08-06 16:38:25
IDEA快速搭建spring boot项目教程(Spring initializr)
2023-08-17 21:11:16
![](https://img.aspxhome.com/file/2023/0/58060_0s.png)
C语言头文件<string.h>函数详解
2023-07-01 18:59:34
![](https://img.aspxhome.com/file/2023/2/105162_0s.png)
Android RecyclerView基本使用详解
2023-07-24 21:13:30
![](https://img.aspxhome.com/file/2023/6/86016_0s.gif)
c#实现输出本月的月历
2023-07-18 19:41:33
AsyncTask官方文档教程整理
2023-07-31 20:25:08
JetBrains 产品输入激活码 Key is invalid 完美解决方案
2023-11-08 08:57:55
![](https://img.aspxhome.com/file/2023/6/58866_0s.jpg)
Flutter 剪裁组件的使用
2023-06-18 13:15:04
![](https://img.aspxhome.com/file/2023/3/96783_0s.png)