linux mysql大小写敏感导致 spring security 无法保存登陆的token

比较坑,使用spring security 的时候,由于使用了remmber-me 记住登陆功能,而且选择的是
PersistentTokenBasedRememberMeServices
来作为token保存service,存储token到数据库中

在开发环境中(windows,MacOs 都正常)部署一直都正常都可以写入到数据库中,但是部署到正式环境(Centos)就炸了写不进去。

后来用在正式服务器上开了tomcat 远程 debug,并断点到 PersistentTokenBasedRememberMeServices 就发现了里面出现异常了(妈蛋 我项目使用的log4j2,而spring 的是log4j ,不知道是不是 log4j2的配置,导致 log4j不会出书东西??),而且特么的不报日志,奇了怪了,断点后发现 是写数据库的时候出错了 说找不到 persistent_logins 表。
然后我就使用 其他的数据库工具连上mysql看下,发现是有表的,但是表名是PERSISTEN_LOGINS ,大写了。。
这下就猜到应该是大小写敏感了,

下面是修改方式:

需要改动 mysql配置文件

  1. 把表名全部改成小写
  2. 修改 /etc/my.cnf 中的 [mysqld]下的lower_case_table_names=1
  3. 重启 mysql
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的基于Spring Security的登录过滤器示例,包含token校验: ```java public class TokenAuthenticationFilter extends OncePerRequestFilter { private final TokenService tokenService; public TokenAuthenticationFilter(TokenService tokenService) { this.tokenService = tokenService; } @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String token = getTokenFromRequest(request); if (StringUtils.hasText(token) && tokenService.validateToken(token)) { Authentication authentication = tokenService.getAuthentication(token); SecurityContextHolder.getContext().setAuthentication(authentication); } filterChain.doFilter(request, response); } private String getTokenFromRequest(HttpServletRequest request) { String bearerToken = request.getHeader("Authorization"); if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) { return bearerToken.substring(7); } return null; } } ``` 这个过滤器会从请求头中获取token,并通过TokenService进行校验。如果token有效,则将认证信息放入SecurityContextHolder中,否则继续执行过滤器链。 在配置Spring Security时,需要将该过滤器添加到过滤器链中: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { private final TokenService tokenService; public SecurityConfig(TokenService tokenService) { this.tokenService = tokenService; } @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated() .and() .addFilterBefore(new TokenAuthenticationFilter(tokenService), UsernamePasswordAuthenticationFilter.class) .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS); } } ``` 在配置中,我们将TokenAuthenticationFilter添加到了UsernamePasswordAuthenticationFilter之前,并且设置了sessionCreationPolicy为STATELESS,使得每次请求都需要进行认证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值