gpt4 book ai didi

java - 无法 Autowiring 。未找到 UserDetailSservice 类型的 Bean

转载 作者:行者123 更新时间:2023-12-02 02:59:27 24 4
gpt4 key购买 nike

这是我运行时的错误详细信息。

APPLICATION FAILED TO START


Description:

Field userDetailsService in com.word.security.WebSecurityConfigrequired 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' inyour 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 显示下面的 userDetailsS​​ervice无法 Autowiring 错误;

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

更新

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 atorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:441)~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE] atorg.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();
}

关于java - 无法 Autowiring 。未找到 UserDetailSservice 类型的 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42562893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com