SpringSecurity里面是没有设置“大小写不敏感”的地方的。所以只有采用我的这个方法。
首先重写UsernamePasswordAuthenticationFilter,因为SpringSecurity的实现实际上就是很多Filter,所以这里还是重写一下
attemptAuthentication(HttpServletRequest request,HttpServletResponse response)的方法。
这个方法里面会读取password并且去验证。
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
if (this.postOnly && !request.getMethod().equals("POST")) {
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
} else {
String username = this.obtainUsername(request);
username = username != null ? username : "";
username = username.trim();
String password = this.obtainPassword(request);
password = password != null ? password : "";
password = toUpperAndToLower.toUpperCase(password);//将接收到的密码全部调整成大写,这里就不上源码了,影响阅读。
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
this.setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
}
然后去下面这个类里面去设定。这个类就是SpringSecurity的设定类。
public class SecurityConfig extends WebSecurityConfigurerAdapter
在这个方法里面:
protected void configure(HttpSecurity http)
下面这段:
//添加自己定义的Filter
http.addFilterBefore(customAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
跳转页面设定保留下面的就可以了,其他删掉:
//设置在没登录的时候访问其他页面的时候系统直接跳转到登录页面
http
.formLogin()
.loginPage("/");
然后在下面这个类里面:
public class SecurityConfig extends WebSecurityConfigurerAdapter
添加:
/**
* 这个方法是自己写了一个AuthenticationFilter。因为原来的系统中的不能够调整用户登录的时候的密码大小写。自豪自己重写。
* @return
* @throws Exception
*/
@Bean
CustomAuthenticationFilter customAuthenticationFilter() throws Exception{
// System.out.println("自己用的过滤器起作用了。");
CustomAuthenticationFilter filter = new CustomAuthenticationFilter();
//如果登录前没有访问地址的话,登录成功之后跳转的页面。这个handler是可以检查登录前页面的功能,如果登录前有访问页面是直接跳转到那个页面的
handler.setDefaultTargetUrl("/index");
filter.setAuthenticationSuccessHandler(handler);//将handler传入拦截器
filter.setAuthenticationFailureHandler(new CustomAuthenticationFailureHandler());
filter.setFilterProcessesUrl("/login");//登录信息提交地址
//这句很关键,重用WebSecurityConfigurerAdapter配置的AuthenticationManager,不然要自己组装AuthenticationManager
filter.setAuthenticationManager(authenticationManagerBean());
return filter;
}
这里面我重写了“AuthenticationFailureHandler”其他我,没动
/**
* 这个就是个登陆后的错误处理类。
*/
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
httpServletResponse.setContentType("application/json;charset=utf-8");
PrintWriter out = httpServletResponse.getWriter();
Map<String, Object> map = new HashMap<>();
map.put("status", 401);
if (e instanceof LockedException) {
map.put("msg", "账户被锁定,登录失败!");
} else if (e instanceof BadCredentialsException) {
map.put("msg", "用户名或密码输入错误,登录失败!");
} else if (e instanceof DisabledException) {
map.put("msg", "账户被禁用,登录失败!");
} else if (e instanceof AccountExpiredException) {
map.put("msg", "账户过期,登录失败!");
} else if (e instanceof CredentialsExpiredException) {
map.put("msg", "密码过期,登录失败!");
} else {
map.put("msg", "登录失败!");
}
out.write(new ObjectMapper().writeValueAsString(map));
out.flush();
out.close();
}
}
好了,现在就没什么问题了。