Я не могу использовать конечные точки пружинного приложения. Это дает мне 403 запретный ответ, кроме одной конечной точки. Я реализовал шестигранную архите. Все потребности удовлетворяются портами. Настроил это в конфигурации безопасности и добавленный поставщик AUTH в цепочке безопасности. За исключением конечной точки /auth /приветствуюсь, я получаю 403 запрещенную ошибку для других конечных точек. < /P>
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(customUserDetailsService);
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
return authConfig.getAuthenticationManager();
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception{
return httpSecurity
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests((auth) -> auth
.anyRequest().permitAll())
.build();
}
// Контроллер
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {
private final RegisterRequestToCommandMapper registerRequestToCommandMapper;
private final LoginRequestToCommandMapper loginRequestToCommandMapper;
private final AuthUseCase authUseCase;
@GetMapping("/Welcome")
public ResponseEntity sayWelcome(){
return ResponseEntity
.status(HttpStatus.OK)
.body("Welcome");
}
@PostMapping("/login")
public ResponseEntity login(@RequestBody LoginRequest request){
LogginResponse logginResponse = authUseCase.login(loginRequestToCommandMapper.toLogginCommand(request));
return ResponseEntity
.status(HttpStatus.OK)
.body(logginResponse);
}
@PostMapping("/register")
public ResponseEntity register(@RequestBody RegisterRequest request){
authUseCase.register(registerRequestToCommandMapper.toRegisterCommand(request));
return ResponseEntity
.status(HttpStatus.CREATED)
.build();
}
}
// Authservice
@Service
@RequiredArgsConstructor
public class AuthUseCaseService implements AuthUseCase {
private final UserRepository userRepository;
private final AuthenticationProvider authenticationProvider;
private final TokenProvider tokenProvider;
private final PasswordEncoderProvider passwordEncoderProvider;
@Override
public void register(RegisterCommand command) {
User userFromRepo = userRepository.findByUserName(command.getUsername()).get();
if(userFromRepo != null){
throw new UsernameAlreadyExistsException(command.getUsername());
}
userRepository.save(User.of(command.getUsername(),command.getFullname(),passwordEncoderProvider.encode(command.getPassword())));
}
@Override
public LogginResponse login(LogginCommand command) {
AuthenticatedUser authenticatedUser = authenticationProvider.authenticate(command);
String token = tokenProvider.getToken(authenticatedUser);
LogginResponse logginResponse = new LogginResponse();
logginResponse.setUsername(authenticatedUser.username());
logginResponse.setToken(token);
logginResponse.setRoles(authenticatedUser.roles());
return logginResponse;
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... g-security
Цепочка фильтров безопасности не работает в Spring Security ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение