2016-11-01 2 views
1

나는 나의 컨트롤러/서비스에봄 부트 보안 익명 인증

SecurityContextHolder.getContext().getAuthentication() 

방법을 사용해야합니다.

이전에 비슷한 방법으로 접근 한 XML 구성 Spring MVC 애플리케이션 (현재는 Java 구성 만 가능)에서 Spring 1.4.1로 마이그레이션했습니다. 항상 null 반환

@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; 
    } 
} 

:

나는이 컨트롤러 예를 들면 SecurityContextHolder.getContext().getAuthentication()을 요구하는 문제를 가지고있다. 익명 인증이 작동하지 않는 이유를 알아낼 수 없습니다.

@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')"); 
    } 

내가 함께 컨트롤러에 @Secured 주석없이 시도 않은 : 여기

봄 보안 구성입니다.

   .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(); 
} 
: 여기

봄 보안 문서에서 예제