2017-06-29 11 views
0

@RequestMappingメソッドを明示的に匿名(許可されていない)アクセスを許可するように定義するにはどうすればよいですか?@RequestMappingで匿名アクセスを許可する方法は?

次は常に401 Unauthorizedを取得し、動作しません:

security.basic.enabled=trueを次のようにアプリケーション全体をspring-bootを使用して、固定されて

@RequestMapping("/test") 
@Secured(value={"ROLE_ANONYMOUS"}) 
public String test() { 
    return "OK"; 
} 

一般的には。

@Configuration 
public class AuthConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private UserDetailsService userDetailsService; 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder()); 
    } 
} 
+1

'@Secured(" ROLE_ANONYMOUS ")'が 'WebSecurityConfigurerAdapter.configure(HttpSecurity httpSecurity)'をすでにオーバーライドしているので、この '@Secured(" ROLE_ANONYMOUS ")は考慮されていない可能性があります。 –

答えて

1

あなたはconfigure(HttpSecurity httpSecurity)メソッドをオーバーライドして、そこに自分のルールを定義することができます

@Configuration 
public class AuthConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private UserDetailsService userDetailsService; 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder()); 
    } 

    @Override 
    protected void configure(HttpSecurity httpSecurity) throws Exception 
    { 
     httpSecurity.authorizeRequests() 
      .antMatchers("/test") 
      .permitAll(); 
     super.configure(http); 
    } 
} 
0
@Configuration 
public class AuthConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private UserDetailsService userDetailsService; 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder()); 
    } 

    @Override 
    protected void configure(HttpSecurity httpSecurity) throws Exception 
    { 
     httpSecurity.authorizeRequests().regexMatcher("^((?!test).)*$").permitAll(); 
    } 
} 

私はtest前にスラッシュわからないですが、ちょうどこの負のルック・アラウンド法を試してみてください。

関連する問題