【发布时间】:2017-01-10 02:34:54
【问题描述】:
我遇到了类似于PreAuthorize annotation doesn't work with jersey 的问题。我为 Spring Security 创建了一个配置类,并且身份验证有效,但授权无效。
这是我的代码
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”角色??
-
这只是原始代码中的一个错字,已修复。还是同样的问题。
标签: java spring rest spring-security jersey