【问题标题】:How to designate public endpoints using custom annotation in spring rest controllers?如何在 spring rest 控制器中使用自定义注释指定公共端点?
【发布时间】: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


【解决方案1】:

你可以使用@PreAuthorize注解作为方法安全

Detail see here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-11
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2011-09-21
    • 2016-02-07
    • 2021-01-08
    相关资源
    最近更新 更多