Я хочу иметь базовое приложение REST с защитой от авторизации. Я выполнил общие инструкции от http://www.baeldung.com/spring-security-authentication-provider, чтобы обеспечить безопасность работы.Пользовательская аутентификацияProvider не называется
Я закончил тем, что создал свою реализацию AuthenticationProvider
, но ее никогда не вызывают весной. Все запросы заканчиваются ошибкой:
{"timestamp":1460199213227,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/test"}
без аутентификацииПродуктор когда-либо делает что-либо.
приложения является аннотацией на основе и здесь соответствующие биты:
настройка безопасности
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Autowired
CustomAuthenticationProvider authenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authenticationProvider(authenticationProvider)
.authorizeRequests()
.anyRequest().authenticated().and().httpBasic();
}
}
AuthenticationProvider
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private UserDAO userDAO;
@Autowired
private Authenticator authenticator;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// This never gets called, I checked with debugger
String username = authentication.getName();
String password = authentication.getCredentials().toString();
User user = userDAO.findByUsername(username);
User authenticatedUser = authenticator.authenticate(user, password);
if (authenticatedUser == null){
throw new RESTAuthenticationException("Auth failed");
}
List<GrantedAuthority> authorityList = new ArrayList<>();
return new UsernamePasswordAuthenticationToken(user, authorityList);
}
@Override
public boolean supports(Class<?> aClass) {
return aClass.equals(UsernamePasswordAuthenticationToken.class);
}
}
Контроллер
@RestController
public class UserController {
@RequestMapping(value = "/test")
public ResponseEntity test(@AuthenticationPrincipal User user) {
return ResponseEntity.ok().body(user);
}
}
В вашем классе @Configuration вы используете DeadlinesAuthenticationProvider, но вы публикуете код CustomAuthenticationProvider. Разве это не твоя проблема? – ben75
Я редактировал код вручную при публикации, так что это просто ошибка с моей стороны, код установлен правильно. Исправлено: –
ОК. Как вы называете/test? Вы используете какой-то инструмент, например, завиток или используете браузер или что-то еще? – ben75