2015-07-12 21 views
6

Oto mój główny config ZastosowanieWiosna bagażnika Bezpieczeństwo Config - authenticationManager musi być określona

@SpringBootApplication 
public class Application { 

    public static void main(String[] args) { 
     new SpringApplicationBuilder(Application.class) 
       .banner((environment, aClass, printStream) -> 
         System.out.println(stringBanner())) 
       .run(); 
    } 
} 

A oto moja aplikacja bezpieczeństwa wiosna config.

@Configuration 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@EnableWebMvcSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private WebServiceAuthenticationEntryPoint unauthorizedHandler; 

    @Autowired 
    private TokenProcessingFilter authTokenProcessingFilter; 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .csrf() 
       .disable() 
       .sessionManagement() 
       .sessionCreationPolicy(SessionCreationPolicy.STATELESS) // Restful hence stateless 
       .and() 
       .exceptionHandling() 
       .authenticationEntryPoint(unauthorizedHandler) // Notice the entry point 
       .and() 
       .addFilter(authTokenProcessingFilter) // Notice the filter 
       .authorizeRequests() 
       .antMatchers("/resources/**", "/api/auth") 
       .permitAll() 
       .antMatchers("/greeting") 
       .hasRole("USER"); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
       .inMemoryAuthentication() 
       .withUser("user") 
       .password("password") 
       .roles("USER"); 
    } 
} 

Oto mój TokenProcessingFilter który rozciąga UsernamePasswordAuthenticationFilter dla mojego filtra niestandardowego uwierzytelniania

@Component 
public class TokenProcessingFilter extends UsernamePasswordAuthenticationFilter { 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
     HttpServletRequest httpRequest = this.getAsHttpRequest(request); 
     String authToken = this.extractAuthTokenFromRequest(httpRequest); 
     String userName = TokenUtils.getUserNameFromToken(authToken); 
     if (userName != null) {/* 
      UserDetails userDetails = userDetailsService.loadUserByUsername(userName);*/ 
      UserDetails userDetails = fakeUserDetails(); 
      if (TokenUtils.validateToken(authToken, userDetails)) { 
       UsernamePasswordAuthenticationToken authentication = 
         new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities()); 
       authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest)); 
       SecurityContextHolder.getContext().setAuthentication(authentication); 
       Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
      } 
     } 
     chain.doFilter(request, response); 
    } 

    private HttpServletRequest getAsHttpRequest(ServletRequest request){ 
     if (!(request instanceof HttpServletRequest)) { 
      throw new RuntimeException("Expecting an HTTP request"); 
     } 
     return (HttpServletRequest) request; 
    } 


    private String extractAuthTokenFromRequest(HttpServletRequest httpRequest) { 
     /* Get token from header */ 
     String authToken = httpRequest.getHeader("x-auth-token"); 
     /* If token not found get it from request parameter */ 
     if (authToken == null) { 
      authToken = httpRequest.getParameter("token"); 
     } 
     return authToken; 
    } 

    private UserDetails fakeUserDetails(){ 
     UsernamePasswordAuthenticationToken authenticationToken = new 
       UsernamePasswordAuthenticationToken("user","password"); 

     List<SimpleGrantedAuthority> auth= new ArrayList<>(); 
     auth.add(new SimpleGrantedAuthority("USER")); 
     return new User("user","password",auth); 
    } 
} 

jednak po uruchomieniu aplikacji, to pojawi się komunikat o wyjątku. czego mi brakuje?

Wystąpił wyjątek podczas pracy. null: InvocationTargetException: Nie można uruchomić osadzonego kontenera; Wyjątkiem jest zagnieżdżony org.springframework.boot.context.embedded.EmbeddedServletContainerException: nie można uruchomić osadzone Tomcat: Błąd tworzenia fasoli nazwą 'tokenProcessingFilter zdefiniowanej w pliku [c: \ Użytkownicy \ kyel \ projekty \ docelowej aplikacji \ \ classes \ org \ app \ testapp \ security \ TokenProcessingFilter.class]: Inwokacja metody init nie powiodła się; wyjątek zagnieżdżonych jest java.lang.IllegalArgumentException: authenticationManager musi być określony

Odpowiedz

10

Musisz ustawić AuthenticationManager na TokenProcessingFilter. Zamiast używać @Component na TokenProcessingFilter, po prostu stwórz go w SecurityConfig.

@Bean 
TokenProcessingFilter tokenProcessingFilter() { 
    TokenProcessingFilter tokenProcessingFilter = new TokenProcessingFilter(); 
    tokenProcessingFilter.setAuthenticationManager(authenticationManager()); 
    return tokenProcessingFilter; 
} 

i

protected void configure(HttpSecurity http) throws Exception { 
    ... 
    .addFilter(tokenProcessingFilter()) 
Powiązane problemy