简体   繁体   English

尝试对用户进行身份验证时发生内部错误

[英]An internal error occurred while trying to authenticate the user

I have created UserDetailsServiceImpl. 我已经创建了UserDetailsS​​erviceImpl。 Here I get the user name and the role of this user. 在这里,我获得了用户名和该用户的角色。 But I couldn't understand what the problem was. 但是我不明白是什么问题。 I've tried the sql query in the database. 我已经尝试了数据库中的sql查询。


SecurityConfig.java SecurityConfig.java

@EnableWebSecurity
@ComponentScan("com")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Bean
public BCryptPasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
};

@Autowired
protected void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}


@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/login", "/", "/veteriner").permitAll().antMatchers("/**")
            .hasRole("ADMIN").and().formLogin().loginPage("/login").defaultSuccessUrl("/login/login-status-success")
            .failureUrl("/login/login-status-error").permitAll().usernameParameter("username")
            .passwordParameter("password").and().logout().logoutSuccessUrl("/logout-success")
            .invalidateHttpSession(true).permitAll().and().csrf();
}

LoginController.java LoginController.java

 @Controller
 @RequestMapping(value = "/login", method = RequestMethod.GET)
 public class LoginController {

 @GetMapping
 public ModelAndView home() throws Exception {
    ModelAndView mv = new ModelAndView();
    mv.setViewName("login");
    return mv;
}

@RequestMapping(path="/login-status-error",method = RequestMethod.GET)
@ResponseBody
public ModelAndView erorLogin() throws Exception {
    ModelAndView mv = new ModelAndView();
    mv.setViewName("main");
    return mv;
}

@RequestMapping(path="/login-status-succes",method = RequestMethod.GET)
@ResponseBody
public ModelAndView succesLogin() throws Exception {
    ModelAndView mv = new ModelAndView();
    mv.setViewName("veteriner");
    return mv;
}

} }


@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

@Autowired
private UserService userService;

@Transactional(readOnly = true)
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    com.model.User user = userService.findByUsername(username);
    if (user == null) {
        throw new UsernameNotFoundException("User not found.");
    }

    User securityUser = new User(user.getUsername(), user.getPassword(), true, true, true, true,
            buildUserAuthority(user.getUserRoles()));
    return securityUser;
}

private List<GrantedAuthority> buildUserAuthority(List<User_role> userRoles) {
    Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();

    for (User_role userRole : userRoles) {
        setAuths.add(new SimpleGrantedAuthority(userRole.getRole().getRoleName()));
    }

    List<GrantedAuthority> results = new ArrayList<GrantedAuthority>(setAuths);
    return results;
}

select user0_.id as id1_6_, user0_.email as email2_6_, user0_.password as password3_6_, user0_.username as username4_6_ from public."User" user0_ where user0_.username=? 从公共位置选择user0_.id作为id1_6_,user0_.email作为email2_6_,user0_.password作为password3_6_,user0_.username作为username4_6_。“用户” user0_其中user0_.username =? 16:37:16.930 [http-nio-8186-exec-12] ERROR org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - An internal error occurred while trying to authenticate the user. 16:37:16.930 [http-nio-8186-exec-12]错误org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter-尝试对用户进行身份验证时发生内部错误。 org.springframework.security.authentication.InternalAuthenticationServiceException: java.util.ArrayList cannot be cast to com.model.User at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:123) ~[spring-security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:144) ~[spring-security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:175) ~[spring-security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:200) ~[spring-security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java org.springframework.security.authentication.InternalAuthenticationServiceException:无法将org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:123)上的java.util.ArrayList强制转换为com.model.User -core-5.1.4.RELEASE.jar:5.1.4.RELEASE],位于org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:144)〜[spring-security-core-5.1.4。 RELEASE.jar:5.1.4.RELEASE],位于org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:175)〜[spring-security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE ] org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:200)〜[spring-security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web .authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java :94) ~[spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:124) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.1.4. :94)〜[spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]在org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)[spring-security -web-5.1.4.RELEASE.jar:5.1.4.RELEASE],位于org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)[spring-security-web-5.1.4.RELEASE .jar:5.1.4.RELEASE]位于org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)[spring-security-web-5.1.4.RELEASE.jar:5.1.4 [RELEASE],位于org.springframework的org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)[spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]。 org.springframework.web.filter.OncePerRequestFilter.doFilter()的security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:124)[spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] OncePerRequestFilter.java:107)[spring-web-5.1.4。 RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at RELEASE.jar:5.1.4.RELEASE],位于org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)[spring-security-web-5.1.4.RELEASE.jar:5.1.4。 RELEASE]位于org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)[spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]位于org.springframework.web .filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java :334)[spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]位于org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)[spring-security- web-5.1.4.RELEASE.jar:5.1.4.RELEASE],位于org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)[spring-security-web-5.1.4.RELEASE。 jar:5.1.4.RELEASE]在 org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178) [spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) [spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.filter.DelegatingFilterProxy org.springframework上的org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)[spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]。 web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]位于org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy。 java:334)[spring-security-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]在org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)[spring-security-web -5.1.4.RELEASE.jar:5.1.4.RELEASE],位于org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)[spring-security-web-5.1.4.RELEASE.jar:5.1 .4.RELEASE]位于org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]位于org.springframework.web。 filter.DelegatingFilterProxy .doFilter(DelegatingFilterProxy.java:270) [spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.16] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.16] at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71) [log4j-web-2.11.1.jar:2.11.1] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.16] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.16] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200) [catalina.jar:9.0.16] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:9.0.16] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [catalina.ja .doFilter(DelegatingFilterProxy.java:270)[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]位于org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[catalina。 jar:9.0.16]位于org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[catalina.jar:9.0.16]位于org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter .java:71)[log4j-web-2.11.1.jar:2.11.1]在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[catalina.jar:9.0.16]在org .apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[catalina.jar:9.0.16]位于org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)[catalina.jar: 9.0.16] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)[catalina.jar:9.0.16] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490 )[catalina.ja r:9.0.16] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [catalina.jar:9.0.16] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [catalina.jar:9.0.16] at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668) [catalina.jar:9.0.16] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [catalina.jar:9.0.16] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [catalina.jar:9.0.16] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-coyote.jar:9.0.16] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:9.0.16] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) [tomcat-coyote.jar:9.0.16] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415) [tomcat-coyote.jar:9.0.16] at org.apache.t r:9.0.16],位于org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)[catalina.jar:9.0.16],位于org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java :92)[catalina.jar:9.0.16] at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)[catalina.jar:9.0.16] at org.apache.catalina.core.StandardEngineValve .invoke(StandardEngineValve.java:74)[catalina.jar:9.0.16]在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)[catalina.jar:9.0.16]在org.apache .coyote.http11.Http11Processor.service(Http11Processor.java:408)[tomcat-coyote.jar:9.0.16]在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)[tomcat-coyote.jar: 9.0.16] at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:834)[tomcat-coyote.jar:9.0.16] at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun (NioEndpoint.java:1415)[tomcat-coyote.jar:9.0.16]在org.apache.t omcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:9.0.16] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_202] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_202] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:9.0.16] at java.lang.Thread.run(Unknown Source) [?:1.8.0_202] Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.model.User at com.dao.UserDaoImpl.findByUsername(UserDaoImpl.java:23) ~[classes/:?] at com.service.UserServiceImpl.findByUsername(UserServiceImpl.java:26) ~[classes/:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_202] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_202] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_202] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_202] at org.springfram omcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)[tomcat-coyote.jar:9.0.16]在java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)[?:1.8.0_202]在Java .util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)[?: 1.8.0_202]在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)[tomcat-util.jar :9.0.16],位于java.lang.Thread.run(未知来源)[?: 1.8.0_202]原因:java.lang.ClassCastException:java.util.ArrayList无法转换为com.com.model.User。 dao.UserDaoImpl.findByUsername(UserDaoImpl.java:23)〜[classes / :?]在com.service.UserServiceImpl.findByUsername(UserServiceImpl.java:26)〜[classes / :?]在sun.reflect.NativeMethodAccessorImpl.invoke0(原始方法)〜[?:1.8.0_202]在sun.reflect.NativeMethodAccessorImpl.invoke(未知源)〜[?:1.8.0_202]在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)〜[?:1.8.0_202 ]在java.lang.reflect.Method.invoke(未知源)〜[?:1.8.0_202]在org.springfram ework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicA ework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)〜[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]在org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation .java:198)〜[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)〜[spring-aop- 5.1.4.RELEASE.jar:5.1.4.RELEASE],位于org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)〜[spring-tx-5.1.4.RELEASE.jar:5.1.4 [RELEASE],位于org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)〜[spring-tx-5.1.4.RELEASE.jar:5.1.4.RELEASE],位于org.springframework.aop.framework .ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)〜[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicA opProxy.java:212) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at com.sun.proxy.$Proxy238.findByUsername(Unknown Source) ~[?:?] at com.service.UserDetailsServiceImpl.loadUserByUsername(UserDetailsServiceImpl.java:29) ~[classes/:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_202] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_202] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_202] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_202] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org opProxy.java:212)〜[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at com.sun.proxy。$ Proxy238.findByUsername(Unknown Source)〜[?:?] at com。 service.UserDetailsS​​erviceImpl.loadUserByUsername(UserDetailsS​​erviceImpl.java:29)〜[classes / :?]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[?:1.8.0_202]在sun.reflect.NativeMethodAccessorImpl.invoke(未知源)〜[?:1.8.0_202]在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)〜[?:1.8.0_202]在java.lang.reflect.Method.invoke(未知源)〜[?:1.8.0_202 ]在org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)〜[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]在org.springframework.aop.framework.ReflectiveMethodInvocation .invokeJoinpoint(ReflectiveMethodInvocation.java:198)〜[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)〜[ spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE]在组织 .springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.4.RELEASE.jar:5.1.4.RELEASE] at com.sun.proxy.$Proxy239.loadUserByUsername(Unknown Source) ~[?:?] at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:108) ~[spring-security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE] ... 45 more .springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)〜[spring-tx-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke( TransactionInterceptor.java:98)〜[spring-tx-5.1.4.RELEASE.jar:5.1.4.RELEASE]在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)〜[spring-aop -5.1.4.RELEASE.jar:5.1.4.RELEASE],位于org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)〜[spring-aop-5.1.4.RELEASE.jar:5.1。 4.RELEASE]位于com.sun.proxy。$ Proxy239.loadUserByUsername(未知来源)〜[?:?] org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:108)〜[spring- security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE] ...另外45


There's too much going on for me to follow, but I think it's simply the case that you can't both create and inject the same bean in the same class. 对于我来说,有太多事情要做,但是我认为这是不能同时在同一个类中创建和注入相同bean的情况。

Ie

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public UserDetailsService userDetailsService() {...}
}

won't work. 不行。 Just think about it, you need UserDetailsService to create the class that's supposed to create UserDetailsService . 试想一下,您需要UserDetailsService来创建应该创建UserDetailsService How would that work? 那会怎么样?

You can do this instead: 你可以这样做:

@EnableWebSecurity
@ComponentScan("com")
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Bean
    public DaoAuthenticationProvider getDaoAuthenticationProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        //call the userDetailsService() method here
        authProvider.setUserDetailsService(userDetailsService());
        authProvider.setPasswordEncoder(this.passwordEncoder());
        return authProvider;
    }

    ...

}

While this looks weird, it does the right thing. 虽然这看起来很奇怪,但它做对了。 Spring proxies all method calls, so when you seemingly call userDetailsService() directly, it actually injects the appropriate bean instead. Spring代理所有方法调用,因此,当您看似直接调用userDetailsService() ,实际上会注入适当的bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 waUsernamePasswordAuthenticationFilter:尝试对用户进行身份验证时发生内部错误 - w.a.UsernamePasswordAuthenticationFilter : An internal error occurred while trying to authenticate the user waUsernamePasswordAuthenticationFilter:尝试验证用户时发生内部错误(ERROR 11759) - w.a.UsernamePasswordAuthenticationFilter : An internal error occurred while trying to authenticate the user (ERROR 11759) 尝试对用户进行身份验证时发生内部错误。 org..InternalAuthenticationServiceException:null - An internal error occurred while trying to authenticate the user. org..InternalAuthenticationServiceException: null 在springboot中尝试使用AuthenticationManager对用户进行身份验证时出现Stackoverflow错误 - Stackoverflow error while trying to authenticate user using AuthenticationManager in springboot 处理脏区时发生内部错误 - An internal error occurred while processing dirty regions 生成SDK时发生内部错误 - Internal error occurred while generating SDK 解析堆转储时发生内部错误 - internal error occurred while parsing heap dump 尝试与sphero连接时发生错误 - An error occurred while trying to connect with the sphero 尝试打开罐子时发生意外错误 - An unexpected error occurred while trying to open jar Android Studio 错误:尝试计算所需的包时发生错误 - Android studio error: An error occurred while trying to compute required packages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM