'spring security fail authentication
I am a beginner with spring boot I want to implement authentication using spring security but when I send a request with postman to authenticate the authentication fails I did a debug and I see that the variable username and password equal a null I think that's the reason, I want to know why these 2 variables equal to null? can you help me
@Configuration
@EnableConfigurationProperties
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
UsersService userService;
@Autowired
PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
// TODO Auto-generated method stub
http.cors().and().csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.POST,"/add-users").permitAll()
.anyRequest()
.authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilter(new AuthenticationFilter(authenticationManagerBean(), userService));
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// TODO Auto-generated method stub
auth.userDetailsService(userService)
.passwordEncoder(passwordEncoder());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
// TODO Auto-generated method stub
return super.authenticationManagerBean();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
public class AuthenticationFilter extends UsernamePasswordAuthenticationFilter{
private String secretKey = "lpmppm";
private final AuthenticationManager authManager;
private final UsersService userservice;
public AuthenticationFilter(AuthenticationManager authManager, UsersService userservice) {
super();
this.authManager = authManager;
this.userservice = userservice;
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
// TODO Auto-generated method stub
String username=request.getParameter("username");
String password = request.getParameter("password");
UsernamePasswordAuthenticationToken authToken=new UsernamePasswordAuthenticationToken(username, password);
return authManager.authenticate(authToken);
}
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
Authentication authResult) throws IOException, ServletException {
// TODO Auto-generated method stub
// super.successfulAuthentication(request, response, chain, authResult);
String token= Jwts.builder()
.setSubject(authResult.getName())
.claim("authorites", authResult.getAuthorities())
.setIssuedAt(new Date())
.setExpiration(java.sql.Date.valueOf(LocalDate.now().plusDays(3)))
.signWith(Keys.hmacShaKeyFor(secretKey.getBytes()))
.compact();
response.addHeader("Authorization","Bearer" + token);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
