spring-security中添加用户过期功能

之前有篇文章记录了我是如何快速添加spring-security模块的,虽然快速,但是功能很简单。最近需要添加一个新的功能,就是验证用户是否过期。
实现方法肯定很多,但是首先要考虑自己项目当前什么个结构。在我的web.xml中,有两个Filter,如下:

<filter>
<filter-name>CodeFilter</filter-name>
<filter-class>com.platform.util.CheckCodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CodeFilter</filter-name>
<url-pattern>/j_spring_security_check</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

第一个filter就是用来验证安全码的,第二个就是spring-security发挥功能的filter。
开始,我打算在CodeFilter中验证用户是否过期。

filterChain.doFilter(request, response);
Object exceptObject = session.getAttribute(
"SPRING_SECURITY_LAST_EXCEPTION");
if (exceptObject != null
&& exceptObject instanceof AccountExpiredException)
response.sendRedirect("login.jsp?errInfo=3");

高人一看就知道这里错了,doFilter之后,是不能再sendRedirect的。我这么想的原因就是springSecurityFilterChain运行之后才会有验证结果,我才好判断是否有过期的异常。
没办法,只能到login.jsp页面去干这件事情了。

session = request.getSession(false);
if(session!=null){
Object exceptObject = session
.getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
if (exceptObject != null
&& exceptObject instanceof AccountExpiredException)
{
out.println("<script language='JavaScript' defer='defer'>");
out.println("alert('用户已过期!');");
out.println("</script>");
}
return;
}


另外我用的是spring-security2.0,好像不支持验证用户过期。下面是JdbcDaoImpl里的一段代码,从这里看,有关是否不过期的变量都被true默认代替了,所以,得自己写一个自己的JdbcDaoImpl。

protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
String username = rs.getString(1);
String password = rs.getString(2);
boolean enabled = rs.getBoolean(3);
UserDetails user = new User(username, password, enabled, true, true, true,
new GrantedAuthority[] {new GrantedAuthorityImpl("HOLDER")});

return user;
}

我写的JdbcDaoImpl取名叫MyJdbcDaoImpl。并且要在applicationContext.xml中进行配置:

<bean id="securityService" class="com.platform.util.MyJdbcDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>

<security:authentication-provider user-service-ref="securityService">
<security:password-encoder hash="md5"/>
</security:authentication-provider>
### Spring Security 集成 Redis 实现会话管理 #### 1. 添加依赖项 为了使Spring Security能够利用Redis来存储和检索会话数据,需引入必要的库文件。通常情况下,在`pom.xml`中加入如下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> ``` 上述代码片段展示了如何向Maven项目添加所需依赖以支持Redis连接以及基于Redis的会话管理[^4]。 #### 2. 配置Redis连接工厂 接下来定义一个Bean用来初始化LettuceConnectionFactory实例,这是推荐的方式因为其社区更加活跃并被Spring Boot默认采用: ```java @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private int redisPort; @Bean public LettuceConnectionFactory redisConnectionFactory() { return new LettuceConnectionFactory(new RedisStandaloneConfiguration(redisHost, redisPort)); } } ``` 这段Java配置类设置了与Redis服务器建立联系所需的参数,并返回了一个新的LettuceConnectionFactory对象给容器使用。 #### 3. 启用HttpSession的支持 为了让HTTP请求之间共享同一个会话状态信息,可以在应用主类上加上@EnableRedisHttpSession注解开启此特性。这使得所有的session都将保存至Redis而非传统的内存方式处理。 ```java @SpringBootApplication @EnableRedisHttpSession(maxInactiveIntervalInSeconds=86400)//设置最大不活动时间为一天 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } } ``` 这里通过指定maxInactiveIntervalInSeconds属性指定了当用户连续多长时间未发起任何请求时即认为该用户的会话已过期,单位为秒[^1]。 #### 4. 自定义SessionRepositoryFilter过滤器 最后一步是注册自定义的SessionRepositoryFilter以便于拦截所有进入系统的HTTP请求并将它们关联到相应的会话记录上去。幸运的是,默认情况下只需简单地声明@EnableRedisHttpSession即可自动完成这项工作;但如果想要进一步定制化行为,则可以通过重写此类来自行实现逻辑。 以上就是关于如何在Spring Security环境中集成Redis来进行高效的会话管理和持久化的全部过程说明。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值