AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
时间: 2024-05-09 17:13:30 浏览: 17
AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)是Shiro框架中一个非常重要的方法,用于在用户登录时进行身份认证。其主要作用是通过传入的token对象获取用户的身份信息,并将其与数据库或其他数据源中的信息进行比较,判断用户是否合法。
该方法一般需要开发者根据自己的业务需求进行重写,例如可以在该方法中编写查询数据库的代码,获取用户的信息。如果查询到该用户的信息,则返回一个封装了用户信息的AuthenticationInfo对象,否则返回null。
需要注意的是,该方法返回的AuthenticationInfo对象中应包含用户的身份信息和密码信息,以便在后续的认证过程中进行比较。
相关问题
spring boot 使用 shiro 1.10.0
要在 Spring Boot 中使用 Shiro 1.10.0,您需要执行以下步骤:
1. 添加 Shiro 依赖项
在 pom.xml 文件中添加以下依赖项:
```xml
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-starter</artifactId>
<version>1.6.0</version>
</dependency>
```
2. 配置 Shiro
在 application.properties 或 application.yml 文件中添加以下 Shiro 配置:
```yaml
shiro:
# 开启 Shiro 的注解支持,例如 @RequiresRoles, @RequiresPermissions 等
enabled: true
# Shiro 过滤器链配置
filter-chain-definition-map:
# 可以在此处添加自定义的过滤器链规则
# /user/** 表示对 user 目录下的所有请求进行拦截,authc 表示需要认证才能访问
/user/**: authc
# Shiro Realm 配置
realms:
# 可以在此处添加自定义的 Realm 类
# realm1 表示 Realm 类的名称,com.example.MyRealm 表示该类的全限定名称
realm1:
class-name: com.example.MyRealm
```
3. 编写 Shiro Realm 类
编写一个自定义的 Realm 类,继承 org.apache.shiro.realm.AuthorizingRealm 类,并重写 doGetAuthorizationInfo 和 doGetAuthenticationInfo 方法,实现权限和认证的逻辑。
```java
public class MyRealm extends AuthorizingRealm {
// 认证逻辑
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// 在此处编写认证逻辑
return null;
}
// 授权逻辑
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
// 在此处编写授权逻辑
return null;
}
}
```
以上就是在 Spring Boot 中使用 Shiro 1.10.0 的基本步骤。需要注意的是,Shiro 的配置非常灵活,可以根据具体的需求进行调整。
为什么账号密码不存在了返回false的时候还是能继续访问
非常抱歉,我之前的回答有误。在Shiro中,当身份验证失败时,应该返回`null`而不是`false`,以使Shiro能够正确处理认证错误并进行相应的处理。
在自定义的Shiro Realm的`doGetAuthenticationInfo()`方法中,如果发现账号密码不存在或者验证失败,应该抛出`UnknownAccountException`或`AuthenticationException`异常,而不是返回`null`。这样Shiro就能够正确地处理异常,并执行相应的操作(例如重定向到登录页面)。
以下是修正后的示例代码:
```java
public class CustomRealm extends AuthorizingRealm {
// ...其他重写的方法...
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
if (!(token instanceof UsernamePasswordToken)) {
return null;
}
UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
String sessionId = usernamePasswordToken.getUsername();
// 从Redis中根据sessionId获取用户密码
Jedis jedis = new Jedis("redis_host", redis_port);
String password = jedis.get(sessionId);
if (password == null) {
throw new UnknownAccountException("Invalid session");
}
// 返回身份验证信息
return new SimpleAuthenticationInfo(sessionId, password, getName());
}
}
```
请确保在自定义Realm中正确抛出异常,并在Shiro配置文件中配置适当的异常处理器来处理身份验证失败的情况,例如重定向到登录页面。
同时,请确保在Shiro的配置文件中正确配置过滤器链和拦截规则,以确保只有在进行身份验证成功后才能继续访问受保护的页面。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)