У меня проблема, похожая на PreAuthorize annotation doesn't work with jersey. Я создал класс конфигурации для Spring Security, и аутентификация работает, но авторизация этого не делает.Использование @PreAuthorize или @Secured с Джерси при использовании класса конфигурации
Вот мой код
SpringSecurityConfig.java
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@Order(1)
@ComponentScan({"com.foo.rest.resources.Template"})
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
private final UserService userService;
private final TokenAuthenticationService tokenAuthenticationService;
public SpringSecurityConfig() {
super(true);
this.userService = new UserService();
tokenAuthenticationService = new TokenAuthenticationService("tooManySecrets", userService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling().and()
.anonymous().and()
.servletApi().and()
.headers().cacheControl().and()
.authorizeRequests()
// Allow anonymous logins
.antMatchers("/auth/**").permitAll()
// All other request need to be authenticated
.anyRequest().authenticated().and()
// Custom Token based authentication based on the header previously given to the client
.addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService),
UsernamePasswordAuthenticationFilter.class);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
@Override
public UserService userDetailsService() {
return userService;
}
@Bean
public TokenAuthenticationService tokenAuthenticationService() {
return tokenAuthenticationService;
}
}
и Template.java
@Component
@Path("/template")
@Produces(MediaType.APPLICATION_JSON)
public class Template {
@GET
@Secured("ROLE_EDITOR")
public User getTemplate() {
return new Template();
}
}
Моя догадка, что аутентификация осуществляется в цепочке фильтров, но он не возвращается после получения тега авторизации. Любая идея, как сделать эту работу?
ли пользователь, доступ к сервису отдыха, сконфигурированный с «ROLE_EDITOR» роль в вашей службе пользователя ?? –
Это только опечатка исходного кода, который был исправлен. Все та же проблема. – user3170736