2016-12-11 55 views
0

我們使用OAuth2來保護我們的REST端點。幾乎我們所有的終端都需要認證。我們有幾個公共端點。我們使用@EnableWebSecurity配置spring安全。所有公共端點都在配置中明確列出(請參見下面的示例中的「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 

回答

相關問題