【发布时间】:2017-04-26 10:08:55
【问题描述】:
我们使用 OAuth2 来保护我们的 REST 端点。几乎我们所有的端点都需要身份验证。我们有几个公共端点。我们使用@EnableWebSecurity 配置spring security。所有公共端点都在配置中明确列出(请参阅下面示例中的“publicpath_x”)。与其在配置中显式添加每个新的公共 enpodint,不如使用自定义注释会容易得多,例如@PublicAccess 将指定每个公共端点。是否可以配置使用此注释注释的端点将被视为公共的,即不需要身份验证?我们不想在路径中指定公共端点(例如,所有公共端点路径都将以“/public”开始/结束)。
安全配置:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
//...
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatchers("publicpath1", "publicpath2").permitAll() //...
}
}
带有自定义注释的公共 REST 控制器示例:
@RestController
public class PublicController1 {
@PublicAccess //our custom annotation
@RequestMapping(value = "publicpath1", method = RequestMethod.GET)
public void publicEndpoint1() {
//...
}
}
我尝试了以下课程但没有成功。
javax.servlet.Filter
org.springframework.web.servlet.handler.HandlerInterceptorAdapter
【问题讨论】:
-
您是仅寻找自定义注释还是使用现有库?
标签: spring rest spring-mvc annotations interceptor