2017-03-02 71 views
0

這是我運行時的錯誤細節。找不到用戶發現的UserDetailService類型的豆類

APPLICATION FAILED TO START


Description:

Field userDetailsService in com.word.security.WebSecurityConfig required a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' in your configuration.

這裏是WebSecurityConfig.java

@Configuration 
    @EnableWebSecurity 
    @Service 
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
@Autowired 
private UserDetailsService userDetailsService; 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    // disable caching 
    http.headers().cacheControl(); 

    http.csrf().disable() // disable csrf for our requests. 
      .authorizeRequests() 
      .antMatchers("/").permitAll() 
      .antMatchers(HttpMethod.GET, "/login").permitAll() 
      .antMatchers(HttpMethod.POST, "/createuser").permitAll() 
      .antMatchers(HttpMethod.GET, "/user/1").permitAll() 
      .anyRequest().authenticated() 
      .and() 
      // We filter the api/login requests 
      .addFilterBefore(new JWTLoginFilter("/login", authenticationManager()), UsernamePasswordAuthenticationFilter.class) 
      // And filter other requests to check the presence of JWT in header 
      .addFilterBefore(new JWTAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); 
} 

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    // Create a default account 
    auth.userDetailsService(userDetailsService()); 
} 
@Bean 
    public UserDetailsService userDetailsService() { 
     return super.userDetailsService(); 
    } 

}

Intellj IDEA顯示Could not Autowire誤差低於userDetailsService;

@Autowired 
private UserDetailsService userDetailsService; 

然而,在我的另一個類名爲SecurityService;

@Service 
public class SecurityService { 
    @Autowired 
    IUserService userService; 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Autowired 
    private UserDetailsService userDetailsService; 

    public User activeUser() { 
     Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
     String username = auth.getName(); 
     Optional<User> user = userService.getUserByName(username); 

     if (user.isPresent()) { 
      return user.get(); 
     } 
     return null; 
    } 
    public void autologin(String username, String password) { 
     UserDetails userDetails = userDetailsService.loadUserByUsername(username); 
     UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, password, AuthorityUtils.createAuthorityList("USER")); 

     authenticationManager.authenticate(usernamePasswordAuthenticationToken); 

     if (usernamePasswordAuthenticationToken.isAuthenticated()) { 
      SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken); 
     } 
    } 
} 

UPDATE

UserController.Class

@RestController 
public class UserController { 
@Autowired 
private IUserService userService; 

@Autowired 
private SecurityService securityService; 

    @RequestMapping(value = "/createuser", method = RequestMethod.POST) 
    public String createUser(@RequestBody User user, Model md) { 
     if (userService.checkExistUserName(user.getUserName())) { 
      md.addAttribute("LoginError", true); 
      return "bu kullanici adi ile bir kullanici bulunmaktadir. Lutfen baska bir kullanici adi ile deneyiniz"; 
     } 
     User newUser = new User(); 
     newUser.setUserName(user.getUserName()); 
     newUser.setFirstname(user.getFirstname()); 
     newUser.setUserMail(user.getUserMail()); 
     newUser.setSurname(user.getSurname()); 
     newUser.setUserPassword(user.getUserPassword()); 
     userService.saveUser(user); 

     /* Automatic login after register */ 
     securityService.autologin(user.getUserName(), user.getUserPassword()); 

     return user.getId().toString(); 
    } 

我沒有得到同樣的錯誤,因爲我有WebSecurityConfig.java

但是現在我嘗試創建用戶後出現下面顯示的StackoverFlow錯誤;

java.lang.StackOverflowError: null at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:441) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~ its like going into the recursive loop. Dont know how to update.

+0

看看這個問題:http://stackoverflow.com/questions/33642199/spring-boot-autowired-not-working-in-configuration-class –

回答

0

WebSecurityConfigurerAdapter包含此方法:

public UserDetailsService userDetailsServiceBean() throws Exception 

從Java文檔:

Override this method to expose a UserDetailsService created from configure(AuthenticationManagerBuilder) as a bean.

因此,嘗試重寫此方法WebSecurityConfig這樣的:

@Bean 
public UserDetailsService userDetailsService() { 
    return super.userDetailsService(); 
} 
+0

謝謝我不再有相同的錯誤。但現在我得到了stackoverflow錯誤。你有什麼想法爲什麼會發生? –

+0

爲什麼我沒有在SecurityService.class中有相同的錯誤 –

0

這個錯誤的原因之一是你沒有這個類的實現。創建一個名爲CustomUserDetailsS​​ervice的類,實現UserDetailsS​​ervice並使用@Component對其進行註釋。 有關更多信息,請參閱spring文檔。

相關問題