Olá! Estou utilizando a versão 2.7.0 do springboot e para esta versão WebSecurityConfigurerAdapter não é mais usado, está deprecado. Dessa maneira, não consigo dar override no método authenticationManager. Não consigo evoluir no módulo 4 (Gerando token jwt) e vídeo 3. Segue o código do SecurityConfigurations.java
@EnableWebSecurity
@Configuration
public class SecurityConfigurations {
@Autowired
PasswordEncoder passwordEncoder;
@Bean
public PasswordEncoder passwordEncoder(){
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationManagerBuilder auth ) throws Exception {
return auth.userDetailsService(userDetailsService())
.passwordEncoder(passwordEncoder()).and().build();
}
@Bean
public InMemoryUserDetailsManager userDetailsService() {
UserDetails user = User.builder()
.passwordEncoder(passwordEncoder::encode)
.username("admin")
.password("admin")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/h2-console/*").permitAll()
.antMatchers("/auth").permitAll()
.anyRequest().authenticated()
.and().csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
return http.build();
}
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.antMatchers("/h2-console/*",
"/configuration/**",
"/swagger-resources/**");
}
}
Ao tentar subir o server, tomo um erro e uma possível solução, como mostrado abaixo:
Description:
Field tokenService in br.com.sankhya.packagerepository.controllers.AutenticacaoController required a bean of type 'br.com.sankhya.packagerepository.config.TokenService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'br.com.sankhya.packagerepository.config.TokenService' in your configuration.
Minha classe TokenService:
public class TokenService {
@Value("${sankhya.package.repository.jwt.expiration}")
private String expiration;
@Value("${sankhya.package.repository.jwt.secret}")
private String secret;
public String gerarToken(Authentication authenticate) {
Usuario logado = (Usuario) authenticate.getPrincipal();
Date hoje = new Date();
Date dataExpiracao = new Date(hoje.getTime() + Long.parseLong(expiration));
return Jwts.builder()
.setIssuer("Spring Introduction")
.setSubject(logado.getId().toString())
.setIssuedAt(hoje)
.setExpiration(dataExpiracao)
.signWith(SignatureAlgorithm.HS256, secret)
.compact();
}
}
Minha classe AutenticacaoControler:
package br.com.sankhya.packagerepository.controllers;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import br.com.sankhya.packagerepository.config.TokenService;
import br.com.sankhya.packagerepository.controllers.dto.TokenDto;
import br.com.sankhya.packagerepository.controllers.form.LoginForm;
@RestController
@RequestMapping("/auth")
public class AutenticacaoController {
@Autowired
private AuthenticationManager authManager;
@Autowired
private TokenService tokenService;
@PostMapping
public ResponseEntity<TokenDto> autenticar(@RequestBody @Valid LoginForm form) {
UsernamePasswordAuthenticationToken dadosLogin = form.converter();
try {
Authentication authenticate = authManager.authenticate(dadosLogin);
String token = tokenService.gerarToken(authenticate);
return ResponseEntity.ok(new TokenDto(token, "Bearer"));
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}
}