【发布时间】:2015-10-31 05:15:34
【问题描述】:
我正在使用 Spring Boot 组合一个 REST api,该 API 将通过 OAuth2 进行保护。我构建了一个安全应用程序,可以很好地管理 jwt 令牌。
我正在组合一个单独的应用程序,它将处理一些一般的用户配置文件资源请求,例如忘记密码、注册和配置文件获取操作。这是用 EnableOAuth2Resource 注释的,它正确地将 OAuth2AuthenticationProcessingFilter 添加到过滤器链中。
@SpringBootApplication
@EnableOAuth2Resource
public class ProfileApplication extends SpringBootServletInitializer {
public static void main(String[] args) throws IOException {
SpringApplication.run(ProfileApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ProfileApplication.class);
}
}
我面临的挑战是我找不到配置安全性的方法,以便对 /profiles 端点的 POST 请求不安全,而对 GET 或 PUT 的请求从提供的不记名令牌传入派生的 @AuthenticationPrincipal。
我想在 API 中设置以下内容 POST /profile 创建一个新用户 - 没有安全性 GET /profile/{id} 通过 id 获取用户 - 需要管理员权限或用户是 authd POST /password/reset - 开始密码重置 - 没有安全性
我有以下 bean 来配置安全性
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(-10) //SecurityProperties.ACCESS_OVERRIDE_ORDER)
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/public/**").permitAll()
.antMatchers(HttpMethod.POST, "/profiles/**").permitAll()
.antMatchers(HttpMethod.GET, "/profiles/**").fullyAuthenticated()
.antMatchers("/password/**").permitAll()
.anyRequest().fullyAuthenticated()
.and()
.csrf().disable();
}
}
由于上述情况,GET 端点调用失败并出现 403 错误,而没有尝试从令牌中查找当前用户,但帖子将通过。查看日志,我不再在过滤器链中看到 OAuth2AuthenticationProcessingFilter 。我尝试添加一些额外的过滤器似乎导致它不再被注册。
这个控制器方法看起来像:
@RequestMapping(method = {RequestMethod.GET}, value="/profiles/{login:.+}")
@ResponseBody
public ResponseEntity<Profile> get(@AuthenticationPrincipal Principal currentUser, @PathVariable String login) {
如果我将顺序设置为 SecurityProperties.ACCESS_OVERRIDE_ORDER 则 GET 请求有效,并且我看到基于 jwt 不记名令牌中的配置文件对我的 oauth 服务的查找,但对配置文件或密码控制器的 POST 请求失败并显示 401 . 所以看起来代码永远不会到达这个过滤器,而是被 OAuth2AuthenticationProcessingFilter 拦截并且请求失败了。
在使用 @EnableOAuth2Resource 时,有没有办法部分保护 Spring Boot 应用程序?我是否必须设置不同的配置 bean 来提供必要的覆盖,如果需要,基于什么接口?
【问题讨论】:
标签: spring spring-security spring-boot spring-security-oauth2