由于不明原因,MyUserDetailsService中的My UsersRepository spring jpa返回null。
那么如何解决在MyUserDetailsService类中自动接线的null?
我尝试了很多方法,但是MyUserDetailsService中的UsersRepository spring jpa始终返回null。
似乎spring不能从调度程序中扫描bean并注入到我的UsersRepository?是真的吗?
这是我的代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import Repository.UsersRepository;
import entity.users;
import securityConfig.MyUserDetails;
@Component
public class MyUserDetailsService implements UserDetailsService{
@Autowired
private UsersRepository usersrepo;
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
try {
Optional<users> user=usersrepo.findByName(userName);
} catch (Exception e) {
System.out.println(e);
}
System.out.println("USER NAME NHAN DC LA: "+userName);
if(!"NhatNam".equalsIgnoreCase(userName)) throw new UsernameNotFoundException("User name not found");
String password = "123";
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_TEACHER");
authorities.add(authority);
MyUserDetails userDetail = new MyUserDetails(userName, password, authorities);
return userDetail;
}
}
我尝试将jpa配置添加到我的spring-security.xml中,并出现错误:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/security"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
<context:component-scan base-package="securityConfig"></context:component-scan>
<jpa:repositories base-package="Repository" />
<beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="persistenceUnitName" value="springdataPU" />
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</beans:property>
</beans:bean>
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<beans:property name="url" value="jdbc:mysql://localhost:3306/quanlykhachsan" />
<beans:property name="username" value="root" />
<beans:property name="password" value="" />
</beans:bean>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>
<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
<beans:bean id="restServicesEntryPoint"
class="securityConfig.RestAuthenticationEntryPoint" />
<beans:bean id="customAccessDeniedHandler"
class="securityConfig.CustomAccessDeniedHandler" />
<beans:bean id="jwtAuthenticationFilter" class="securityConfig.JwtAuthenticationTokenFilter">
<beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>
<beans:bean id="corsHandler" class="securityConfig.CorsFilter" />
<http pattern="/Auth/**" auto-config="false" use-expressions="true"
create-session="stateless" entry-point-ref="restServicesEntryPoint" >
<intercept-url pattern="/CustomerMamagement/**" access="hasRole('ROLE_TEACHER')" />
<form-login login-page="/login.html" login-processing-url="/Auth/login" username-parameter="username" password-parameter="password"
authentication-success-forward-url="/Auth/signUp"
authentication-failure-forward-url="/error"
/>
<logout logout-url="/Auth/logout" />
<csrf disabled="true"/>
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="myUserDetailsService">
</authentication-provider>
</authentication-manager>
<beans:bean id="myUserDetailsService" class="securityConfig.MyUserDetailsService">
错误:
INFO: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1329)
at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1004)
at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.checkContextClassLoaders(AbandonedConnectionCleanupThread.java:96)
at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:69)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>AuthDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/AuthDispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>AuthDispatcher</servlet-name>
<url-pattern>/Auth/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CustomerMamagementDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/CustomerMamagementDispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>CustomerMamagementDispatcher</servlet-name>
<url-pattern>/CustomerMamagement/*</url-pattern>
</servlet-mapping>
<!-- SPRING SECURITY -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--END SPRING SECURITY -->
</web-app>