2016-12-18 100 views
0

我试图通过引入PreInvocationAuthorizationAdvice来实现我自己的授权机制。这里是我的代码:为什么我的PreInvocationAuthorizationAdvice.before未被调用?

我的SecurityContext:

我SecurityAdapter:

@Configuration 
@EnableWebSecurity 
public class SecurityAdapter extends WebSecurityConfigurerAdapter 
{ 
    @Override 
    protected void configure(HttpSecurity http) 
     throws Exception 
    { 
     http 
      .authorizeRequests() 
      .anyRequest().permitAll(); 
     http 
      .csrf() 
       .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()); 
    } 
} 

最后MyPreInvocationAdvice

public class MyPreInvocationAdvice implements PreInvocationAuthorizationAdvice 
{ 
    public MyPreInvocationAdvice() 
    { 
    } 

    @Override 
    public boolean before(Authentication authentication, MethodInvocation methodInvocation, PreInvocationAttribute preInvocationAttribute) 
    { 
     return true; 
    } 
} 

在这一刻,我授权的所有请求。但问题是,当我提出请求时,根本不会调用before方法。有人可以告诉我我犯了什么错误吗?

回答

0

我自己找到了答案,所以我可以在将来自己参考,在这里。

您的控制器需要注明@PreAuthorize("")。如果你以后不想使用它,那么String值并不重要(这将是你的代码使用它,所以如果你不想使用它,就放弃它)。

@RestController 
@PreAuthorize("") 
public class Controller 
{ 
} 
相关问题