Я разрабатываю приложение Spring Boot, использующее Spring Security для проверки успешности входа пользователя в систему, или пользователь авторизован для доступа к ресурсу внутри.
Прямо сейчас всегда возвращается 401 Несанкционировано, введу ли я неправильное имя пользователя / пароль или пользователь заблокирован. Я хочу настроить его, скажем, если неправильные учетные данные вернут 400, а если пользователь заблокирован, вернут 401.
Я определил пользовательский фильтр и точку входа для аутентификации для приложения.
public class JWTUsernameAndPasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
....
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
Optional<UserEntity> userEntity = userService
.findByUserId(request.getParameter("username"));
if (userEntity.isPresent()) {
if (userEntity.get().getStatus().equals("LOCKED")) {
throw new BadCredentialsException("User is locked!"); //I want it to return status 403 here
}
}
String privateKey = userService.findByUserId(request.getParameter("username")).get().getLoginKey();
String username = request.getParameter("username");
String password = CryptoUtil.decrypt(privateKey, request.getParameter("password"));
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username,
password, Collections.emptyList());
return authenticationManager.authenticate(authenticationToken);
}
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
AuthenticationException failed) throws IOException, ServletException {
String username = request.getParameter("username");
try {
// Do some process stuff....
} catch (Exception ex) {
log.error(ex);
}
super.unsuccessfulAuthentication(request, response, failed); // I want to return 400 here
}
}
public class JwtAuthenticationExceptionEntryPoint implements AuthenticationEntryPoint {
@Getter
@Value("${application.version}")
private String version;
@Getter
@Value("${application.name}")
private String applicationName;
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
RESTFulStatus status = new RESTFulStatus(RESTFulStatus.STATUS_ERROR, HttpStatus.UNAUTHORIZED.value(),
HttpStatus.UNAUTHORIZED.getReasonPhrase(),
authException.getMessage(), version, applicationName, Instant.now(), request.getRequestURI());
log.error(authException.getMessage());
ObjectMapper mapper = new ObjectMapper();
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(mapper.writeValueAsString(status));
}
}
Конфигурационный класс
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthenticationExceptionEntryPoint authenticationEntryPoint;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().cacheControl();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.csrf().disable();
http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).and()
.addFilter(new JWTUsernameAndPasswordAuthenticationFilter(authenticationManager(), jwtConfig, userService, accessHistoryService))
.addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtConfig)).authorizeRequests();
http.authorizeRequests().antMatchers(HttpMethod.POST, jwtConfig.getUri()).permitAll()
.antMatchers("/auth/**").permitAll()
.antMatchers("/1/**").hasRole(RoleConstant.ROLE_ADMIN)
.antMatchers("/test/**").hasAnyRole(RoleConstant.ROLE_ADMIN)
.antMatchers("/report/**").hasAnyRole(RoleConstant.ROLE_ADMIN, RoleConstant.ROLE_REPORT)
.anyRequest().authenticated();
// If a user try to access a resource without having enough permissions
http.exceptionHandling().accessDeniedPage("/login");
//test in browser
http.httpBasic();
}
}