setSessionValidationSchedulerEnabled作用
时间: 2024-04-23 18:28:06 浏览: 155
setSessionValidationSchedulerEnabled是Shiro中的一个方法,用于启用或禁用会话验证调度器。当会话验证调度器启用时,Shiro将定期执行会话验证操作,以确保会话的有效性和安全性。
会话验证是指在每次请求时,Shiro会检查当前会话是否过期或被标记为无效。如果会话过期或无效,Shiro将触发相应的操作,比如重定向到登录页面或执行其他自定义操作。
通过调用setSessionValidationSchedulerEnabled方法,并传入true或false参数,可以控制是否启用会话验证调度器。启用后,Shiro将根据配置的时间间隔定期执行会话验证操作。禁用后,将不会进行会话验证操作。
示例代码:
```java
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
sessionManager.setSessionValidationSchedulerEnabled(true); // 启用会话验证调度器
securityManager.setSessionManager(sessionManager);
```
通过设置setSessionValidationSchedulerEnabled(true),可以启用会话验证调度器,确保会话的有效性和安全性。
相关问题
Java使用shiro完成用户登录认证功能
Shiro是一个强大的Java安全框架,可以用于认证、授权、加密、会话管理等方面。下面是使用Shiro完成用户登录认证功能的步骤:
1. 引入Shiro依赖
在项目的pom.xml中引入Shiro的依赖:
```
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.7.1</version>
</dependency>
```
2. 配置Shiro
在项目的配置文件中配置Shiro,包括Realm、Session管理器、密码比较器等:
```
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
shiroFilterFactoryBean.setLoginUrl("/login");
shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
filterChainDefinitionMap.put("/css/**", "anon");
filterChainDefinitionMap.put("/js/**", "anon");
filterChainDefinitionMap.put("/images/**", "anon");
filterChainDefinitionMap.put("/login", "anon");
filterChainDefinitionMap.put("/**", "authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(realm());
securityManager.setSessionManager(sessionManager());
return securityManager;
}
@Bean
public Realm realm() {
CustomRealm realm = new CustomRealm();
realm.setCredentialsMatcher(credentialsMatcher());
return realm;
}
@Bean
public SessionManager sessionManager() {
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
sessionManager.setGlobalSessionTimeout(30 * 60 * 1000);
sessionManager.setDeleteInvalidSessions(true);
sessionManager.setSessionValidationSchedulerEnabled(true);
sessionManager.setSessionIdCookieEnabled(true);
sessionManager.setSessionIdCookie(sessionIdCookie());
return sessionManager;
}
@Bean
public SimpleCookie sessionIdCookie() {
SimpleCookie cookie = new SimpleCookie("JSESSIONID");
cookie.setHttpOnly(true);
cookie.setMaxAge(-1);
return cookie;
}
@Bean
public CredentialsMatcher credentialsMatcher() {
HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
credentialsMatcher.setHashAlgorithmName("SHA-256");
credentialsMatcher.setHashIterations(1);
return credentialsMatcher;
}
```
3. 自定义Realm
实现自己的Realm,用于认证用户的身份和权限:
```
public class CustomRealm extends AuthorizingRealm {
@Autowired
private UserService userService;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = (String) principals.getPrimaryPrincipal();
User user = userService.findByUsername(username);
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
authorizationInfo.setRoles(user.getRoles());
authorizationInfo.setStringPermissions(user.getPermissions());
return authorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
String username = upToken.getUsername();
User user = userService.findByUsername(username);
if (user == null) {
throw new UnknownAccountException();
}
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, user.getPassword(), getName());
return authenticationInfo;
}
}
```
4. 编写登录页面和处理登录请求的Controller
在登录页面中,用户可以输入用户名和密码,登录请求提交到后端进行处理:
```
@GetMapping("/login")
public String login() {
return "login";
}
@PostMapping("/login")
public String doLogin(String username, String password) {
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);
return "redirect:/home";
} catch (AuthenticationException e) {
return "login";
}
}
```
5. 编写注销功能
在需要注销的页面中,用户可以点击注销按钮,请求提交到后端进行处理:
```
@GetMapping("/logout")
public String logout() {
Subject subject = SecurityUtils.getSubject();
subject.logout();
return "redirect:/login";
}
```
完成了以上步骤,就可以使用Shiro完成用户登录认证功能了。
springboot+shiro的框架怎么写呢,我的maven是:<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web-starter</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.18.2</version> </dependency>
好的,对于使用 Spring Boot 和 Shiro 框架的项目,可以参照以下步骤进行搭建:
1. 在 pom.xml 文件中添加依赖:
```
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-web-starter</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.18.2</version>
</dependency>
```
其中,shiro-spring-boot-web-starter 是 Shiro 在 Spring Boot 中的集成包,java-jwt 是用于生成和解析 JWT 的库。
2. 在 application.properties 或 application.yml 文件中配置 Shiro:
```
# 配置 Shiro 的加密方式
shiro.hashAlgorithmName=md5
shiro.hashIterations=2
# 配置 JWT 的过期时间
jwt.expireTime=1800
# 配置 Shiro 的登录路径和首页路径
shiro.loginUrl=/login
shiro.successUrl=/index
# 配置 Shiro 的过滤器链
shiro.filterChainDefinitionMap= \
/static/** = anon \
/login = anon \
/logout = logout \
/** = authc
```
其中,shiro.hashAlgorithmName 和 shiro.hashIterations 是 Shiro 的加密方式,jwt.expireTime 是 JWT 的过期时间,shiro.loginUrl 和 shiro.successUrl 是 Shiro 的登录路径和首页路径,shiro.filterChainDefinitionMap 是 Shiro 的过滤器链。
3. 创建 Shiro 配置类,用于配置 Shiro 的各种组件:
```
@Configuration
public class ShiroConfig {
// 配置 SecurityManager
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(myRealm()); // 配置自定义 Realm
securityManager.setRememberMeManager(cookieRememberMeManager()); // 配置 RememberMeManager
securityManager.setSessionManager(sessionManager()); // 配置 SessionManager
return securityManager;
}
// 配置自定义 Realm
@Bean
public MyRealm myRealm() {
return new MyRealm();
}
// 配置 RememberMeManager
@Bean
public RememberMeManager cookieRememberMeManager() {
CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
rememberMeManager.setCookie(rememberMeCookie());
return rememberMeManager;
}
// 配置 RememberMeCookie
@Bean
public SimpleCookie rememberMeCookie() {
SimpleCookie cookie = new SimpleCookie("rememberMe");
cookie.setMaxAge(86400); // 设置 Cookie 的过期时间为一天
return cookie;
}
// 配置 SessionManager
@Bean
public DefaultWebSessionManager sessionManager() {
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
sessionManager.setSessionIdCookieEnabled(true);
sessionManager.setSessionIdUrlRewritingEnabled(false);
sessionManager.setSessionDAO(redisSessionDAO()); // 配置 RedisSessionDAO
sessionManager.setGlobalSessionTimeout(1800000); // 设置 Session 的过期时间为半小时
sessionManager.setDeleteInvalidSessions(true);
sessionManager.setSessionValidationSchedulerEnabled(true);
return sessionManager;
}
// 配置 RedisSessionDAO
@Bean
public RedisSessionDAO redisSessionDAO() {
RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
redisSessionDAO.setRedisManager(redisManager()); // 配置 RedisManager
redisSessionDAO.setKeyPrefix("shiro:session:");
redisSessionDAO.setExpire(1800); // 设置 Session 的过期时间为半小时
return redisSessionDAO;
}
// 配置 RedisManager
@Bean
public RedisManager redisManager() {
RedisManager redisManager = new RedisManager();
redisManager.setHost("localhost");
redisManager.setPort(6379);
redisManager.setTimeout(0);
return redisManager;
}
// 配置 ShiroFilterFactoryBean
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean() {
ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
factoryBean.setSecurityManager(securityManager());
factoryBean.setLoginUrl("/login");
factoryBean.setSuccessUrl("/index");
factoryBean.setUnauthorizedUrl("/unauthorized");
factoryBean.setFilterChainDefinitionMap(shiroFilterChainDefinition().getFilterChainMap());
return factoryBean;
}
// 配置 ShiroFilterChainDefinition
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
chainDefinition.addPathDefinition("/static/**", "anon");
chainDefinition.addPathDefinition("/login", "anon");
chainDefinition.addPathDefinition("/logout", "logout");
chainDefinition.addPathDefinition("/**", "authc");
return chainDefinition;
}
}
```
其中,myRealm() 方法用于配置自定义 Realm,cookieRememberMeManager() 方法用于配置 RememberMeManager 和 RememberMeCookie,sessionManager() 方法用于配置 SessionManager 和 RedisSessionDAO,redisManager() 方法用于配置 RedisManager,shiroFilterFactoryBean() 方法用于配置 ShiroFilterFactoryBean,shiroFilterChainDefinition() 方法用于配置 ShiroFilterChainDefinition。
4. 创建自定义 Realm 类,用于实现用户的认证和授权逻辑:
```
public class MyRealm extends AuthorizingRealm {
// 配置加密方式
@Override
public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
matcher.setHashAlgorithmName("md5");
matcher.setHashIterations(2);
super.setCredentialsMatcher(matcher);
}
// 用户认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
String username = upToken.getUsername();
String password = new String(upToken.getPassword());
// 根据用户名和密码查询用户信息
User user = userService.findByUsernameAndPassword(username, password);
if (user == null) {
throw new UnknownAccountException("用户名或密码错误");
}
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName());
return info;
}
// 用户授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
User user = (User) principals.getPrimaryPrincipal();
// 根据用户角色查询权限信息
List<Role> roles = userService.findRolesByUsername(user.getUsername());
for (Role role : roles) {
info.addRole(role.getName());
List<Permission> permissions = role.getPermissions();
for (Permission permission : permissions) {
info.addStringPermission(permission.getName());
}
}
return info;
}
}
```
在自定义 Realm 类中,通过 setCredentialsMatcher() 方法配置加密方式,通过 doGetAuthenticationInfo() 方法实现用户的认证逻辑,通过 doGetAuthorizationInfo() 方法实现用户的授权逻辑。
5. 创建登录控制器,用于处理用户的登录和注销:
```
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
@PostMapping("/login")
public String login(String username, String password, boolean rememberMe, HttpServletRequest request) {
try {
// 将用户名和密码封装成 UsernamePasswordToken 对象
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
// 设置 RememberMe
token.setRememberMe(rememberMe);
// 执行登录操作
SecurityUtils.getSubject().login(token);
return "redirect:/index";
} catch (UnknownAccountException e) {
request.setAttribute("error", "用户名不存在");
} catch (IncorrectCredentialsException e) {
request.setAttribute("error", "密码错误");
} catch (LockedAccountException e) {
request.setAttribute("error", "账号已被锁定");
} catch (AuthenticationException e) {
request.setAttribute("error", "登录失败:" + e.getMessage());
}
return "login";
}
@GetMapping("/logout")
public String logout() {
SecurityUtils.getSubject().logout();
return "redirect:/login";
}
}
```
在登录控制器中,通过 login() 方法实现用户的登录操作,通过 logout() 方法实现用户的注销操作。
6. 在页面中添加登录界面和首页,例如:
```
<!-- 登录界面 -->
<form method="post" action="/login">
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" class="form-control" required>
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" id="password" name="password" class="form-control" required>
</div>
<div class="form-check">
<input type="checkbox" id="rememberMe" name="rememberMe" class="form-check-input">
<label for="rememberMe" class="form-check-label">记住我</label>
</div>
<button type="submit" class="btn btn-primary">登录</button>
</form>
<!-- 首页 -->
<h1>欢迎登录</h1>
<p><a href="/logout">退出登录</a></p>
```
通过以上步骤,就可以使用 Spring Boot 和 Shiro 框架来实现用户的认证和授权了,当然,在具体实现时,还需要针对自己的业务需求进行一些调整和优化。
阅读全文