无法 Autowire.No 找到 UserDetailService 类型的 Bean

Could not Autowire.No Beans of UserDetailService type found

这是我运行.

时的错误详情

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.javaclass

@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 在下方显示 userDetailsServiceCould not Autowire 错误;

@Autowired
private UserDetailsService userDetailsService;

但是,在我另一个名为 SecurityService 的 class 上;

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

更新

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.

相同的错误

但现在我在尝试创建用户后收到如下所示的 Whosebug 错误;

java.lang.WhosebugError: 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.

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();
}

出现此错误的原因之一是您没有实现此 class。创建一个名为 CustomUserDetailsS​​ervice 的 class 实现 UserDetailsS​​ervice 并用 @Component 注释它。 有关更多信息,请参阅 spring 文档。