2016-11-01 16 views
1

春ブーツセキュリティ匿名認証

私はXMLで構成されたSpring MVCアプリケーション(現在はJava configsのみ)からSpring Boot 1.4.1にマイ・バージョンを移行しました。

私はこのコントローラでは、たとえば、SecurityContextHolder.getContext().getAuthentication()を呼び出す問題があります。

@RestController 
@Secured("IS_AUTHENTICATED_ANONYMOUSLY") 
@RequestMapping("utils") 
public class UtilsController { 
    @RequestMapping(value = "/check_auth", method = RequestMethod.GET) 
    public Boolean getAuthState() throws SQLException { 
     if (SecurityContextHolder.getContext().getAuthentication() == null){ 
      logger.info("Auth obj null"); 
     } 

     if (SecurityContextHolder.getContext().getAuthentication().getName() != null && SecurityContextHolder.getContext().getAuthentication().getName() != "anonymousUser") { 
      return true; 
     } else return false; 
    } 
} 

それは常にnullを返します。匿名認証が機能しない理由を理解できません。私が試したし、コントローラ上の@Secured注釈なしでした

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).and() 
       .formLogin() 
        .successHandler(ajaxSuccessHandler) 
        .failureHandler(ajaxFailureHandler) 
        .loginProcessingUrl("/authentication") 
        .passwordParameter("password") 
        .usernameParameter("username") 
        .and() 
       .logout() 
        .deleteCookies("JSESSIONID") 
        .invalidateHttpSession(true) 
        .logoutUrl("/logout") 
        .logoutSuccessUrl("/") 
        .and() 
       .csrf().disable() 
        // .anonymous().disable() 
       .authorizeRequests() 
        // .anyRequest().anonymous() 
        .antMatchers("/utils").permitAll() 
        .antMatchers("/oauth/token").permitAll() 
        .antMatchers("/admin/*").access("hasRole('ROLE_ADMIN')") 
        .antMatchers("/user/*").access("hasRole('ROLE_USER')"); 
    } 

:ここ

春のセキュリティ設定です。

   .authorizeRequests() 
        // .anyRequest().anonymous() 
        .antMatchers("/utils").permitAll() 

この設定ではさまざまなバリエーションがあります。あなたはセキュリティ設定あなたの中に認証されていないため、

SecurityContextHolder.getContext().getAuthentication() 

答えて

2

あなたがしてnullを得ています。

あなたは、単純なを追加することができます:あなたがフォーム・ログインを使用している

.authenticated()             
      .and() 
     // ... 
     .formLogin(); 

場合。

各リクエストを認証した後、null以外のものを取得するとします。ここで

は春のセキュリティのドキュメントからの例です:

protected void configure(HttpSecurity http) throws Exception { 
    http 
     .authorizeRequests()                 
      .antMatchers("/resources/**", "/signup", "/about").permitAll()     
      .antMatchers("/admin/**").hasRole("ADMIN")          
      .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")    
      .anyRequest().authenticated()             
      .and() 
     // ... 
     .formLogin(); 
} 
関連する問題