我调试了UserDetailsServiceImpl的loadUserByUsername方法,但是当我提交登录表单时谷歌它没有被调用,并且没有解决方案适用于我。
的login.jsp
<form autocomplete="on" method="POST" id="loginForm"
action="j_spring_security_check">
<h1>Log in</h1>
<p>
<label for="email" class="uname" data-icon="u"> Email </label> <input
id="j_username" name="j_username" required="required"
type="text" placeholder="mymail@mail.com" />
</p>
<p>
<label for="password" class="youpasswd" data-icon="p">
Password </label> <input id="j_password" name="j_password"
required="required" type="password" placeholder="eg. X8df!90EO" />
</p>
<p class="keeplogin">
<input type="checkbox" name="loginkeeping" id="loginkeeping"
value="loginkeeping" /> <label for="loginkeeping">Keep
me logged in</label>
</p>
<p class="login button">
<input type="submit" value="Login" id="j_submitLogin" />
</p>
<p class="change_link">
Not register yet ? <a href="register.html" class="to_register">Register
now</a>
</p>
</form>
安全-context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.fyp.ptma" />
<beans:bean id="loggerListener"
class="org.springframework.security.authentication.event.LoggerListener" />
<global-method-security pre-post-annotations="enabled" />
<http pattern="/resources/**" security="none" />
<http use-expressions="true" auto-config="true">
<intercept-url pattern="/**" access="permitAll" />
<intercept-url pattern="/secure/*" access="isAuthenticated()" />
<form-login login-page="/login.html" default-target-url="/secure/fileUpload.html"
authentication-failure-url="/login.html?error=true"
login-processing-url="/j_spring_security_check"
authentication-success-handler-ref="loginSuccessHandler"
always-use-default-target="false" />
<logout />
<remember-me user-service-ref="userDetailsService" />
<access-denied-handler error-page="/login.html?error=true" />
</http>
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder hash="md5" />
</authentication-provider>
</authentication-manager>
<beans:bean id="userDetailsService"
class="com.fyp.ptma.dao.service.UserDetailsServiceImpl">
</beans:bean>
<beans:bean id="loginSuccessHandler" class="com.fyp.ptma.security.LoginSuccessHandler" />
</beans:beans>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/log4j.properties</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/appServlet/security-context.xml
/WEB-INF/hibernate/hibernate-config.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>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>
Spring Security Log
20:32:57,569 DEBUG [org.springframework.security.web.util.AntPathRequestMatcher] - Checking match of request : '/'; against '/resources/**'
20:32:57,570 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 1 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
20:32:57,571 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - No HttpSession currently exists
20:32:57,571 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - No SecurityContext was available from the HttpSession: null. A new one will be created.
20:32:57,573 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 2 of 11 in additional filter chain; firing Filter: 'LogoutFilter'
20:32:57,573 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 3 of 11 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
20:32:57,573 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 4 of 11 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
20:32:57,573 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 5 of 11 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
20:32:57,573 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 6 of 11 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
20:32:57,574 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 7 of 11 in additional filter chain; firing Filter: 'RememberMeAuthenticationFilter'
20:32:57,574 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 8 of 11 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
20:32:57,575 DEBUG [org.springframework.security.web.authentication.AnonymousAuthenticationFilter] - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@6faaf9b0: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffff8868: RemoteIpAddress: 0:0:0:0:0:0:0:1%0; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
20:32:57,575 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 9 of 11 in additional filter chain; firing Filter: 'SessionManagementFilter'
20:32:57,575 DEBUG [org.springframework.security.web.session.SessionManagementFilter] - Requested session ID 1x8wca5l9e1m31npsvhg4wkyv7 is invalid.
20:32:57,575 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 10 of 11 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
20:32:57,575 DEBUG [org.springframework.security.web.FilterChainProxy] - / at position 11 of 11 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
20:32:57,576 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - Secure object: FilterInvocation: URL: /; Attributes: [permitAll]
20:32:57,576 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@6faaf9b0: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffff8868: RemoteIpAddress: 0:0:0:0:0:0:0:1%0; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
20:32:57,576 DEBUG [org.springframework.security.access.vote.AffirmativeBased] - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@3a34c0bd, returned: 1
20:32:57,576 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - Authorization successful
20:32:57,576 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - RunAsManager did not change Authentication object
20:32:57,577 DEBUG [org.springframework.security.web.FilterChainProxy] - / reached end of additional filter chain; proceeding with original chain
20:32:58,040 DEBUG [org.springframework.security.web.access.ExceptionTranslationFilter] - Chain processed normally
20:32:58,041 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
20:32:58,041 DEBUG [org.springframework.security.web.context.SecurityContextPersistenceFilter] - SecurityContextHolder now cleared, as request processing completed
20:33:03,194 DEBUG [org.springframework.security.web.util.AntPathRequestMatcher] - Checking match of request : '/login.html'; against '/resources/**'
20:33:03,195 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 1 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
20:33:03,196 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - No HttpSession currently exists
20:33:03,196 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - No SecurityContext was available from the HttpSession: null. A new one will be created.
20:33:03,196 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 2 of 11 in additional filter chain; firing Filter: 'LogoutFilter'
20:33:03,196 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 3 of 11 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
20:33:03,196 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 4 of 11 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
20:33:03,196 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 5 of 11 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
20:33:03,196 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 6 of 11 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
20:33:03,196 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 7 of 11 in additional filter chain; firing Filter: 'RememberMeAuthenticationFilter'
20:33:03,197 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 8 of 11 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
20:33:03,197 DEBUG [org.springframework.security.web.authentication.AnonymousAuthenticationFilter] - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@6faaf9b0: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffff8868: RemoteIpAddress: 0:0:0:0:0:0:0:1%0; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
20:33:03,197 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 9 of 11 in additional filter chain; firing Filter: 'SessionManagementFilter'
20:33:03,197 DEBUG [org.springframework.security.web.session.SessionManagementFilter] - Requested session ID 1x8wca5l9e1m31npsvhg4wkyv7 is invalid.
20:33:03,199 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 10 of 11 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
20:33:03,199 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html at position 11 of 11 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
20:33:03,199 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - Secure object: FilterInvocation: URL: /login.html; Attributes: [permitAll]
20:33:03,199 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@6faaf9b0: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffff8868: RemoteIpAddress: 0:0:0:0:0:0:0:1%0; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
20:33:03,199 DEBUG [org.springframework.security.access.vote.AffirmativeBased] - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@3a34c0bd, returned: 1
20:33:03,199 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - Authorization successful
20:33:03,200 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - RunAsManager did not change Authentication object
20:33:03,200 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html reached end of additional filter chain; proceeding with original chain
20:33:03,225 DEBUG [org.springframework.security.web.access.ExceptionTranslationFilter] - Chain processed normally
20:33:03,226 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
20:33:03,226 DEBUG [org.springframework.security.web.context.SecurityContextPersistenceFilter] - SecurityContextHolder now cleared, as request processing completed
20:33:07,738 DEBUG [org.springframework.security.web.util.AntPathRequestMatcher] - Checking match of request : '/j_spring_security_check'; against '/resources/**'
20:33:07,739 DEBUG [org.springframework.security.web.FilterChainProxy] - /j_spring_security_check at position 1 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
20:33:07,739 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - No HttpSession currently exists
20:33:07,739 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - No SecurityContext was available from the HttpSession: null. A new one will be created.
20:33:07,739 DEBUG [org.springframework.security.web.FilterChainProxy] - /j_spring_security_check at position 2 of 11 in additional filter chain; firing Filter: 'LogoutFilter'
20:33:07,739 DEBUG [org.springframework.security.web.FilterChainProxy] - /j_spring_security_check at position 3 of 11 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
20:33:07,739 DEBUG [org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter] - Request is to process authentication
20:33:07,744 DEBUG [org.springframework.security.authentication.ProviderManager] - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider
20:33:07,968 WARN [org.springframework.security.authentication.event.LoggerListener] - Authentication event AuthenticationFailureServiceExceptionEvent: asdfasdfaasdf; details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffff8868: RemoteIpAddress: 0:0:0:0:0:0:0:1%0; SessionId: null; exception: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection
20:33:07,968 DEBUG [org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter] - Authentication request failed: org.springframework.security.authentication.AuthenticationServiceException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection
20:33:07,968 DEBUG [org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter] - Updated SecurityContextHolder to contain null Authentication
20:33:07,968 DEBUG [org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter] - Delegating to authentication failure handlerorg.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler@2aca6d70
20:33:07,968 DEBUG [org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices] - Interactive login attempt was unsuccessful.
20:33:07,968 DEBUG [org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices] - Cancelling cookie
20:33:07,973 DEBUG [org.springframework.security.web.session.HttpSessionEventPublisher] - Publishing event: org.springframework.security.web.session.HttpSessionCreatedEvent[source=org.mortbay.jetty.servlet.HashSessionManager$Session:qvnxffio0znw@693465617]
20:33:07,974 DEBUG [org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler] - Redirecting to /login.html?error=true
20:33:07,976 DEBUG [org.springframework.security.web.DefaultRedirectStrategy] - Redirecting to '/ptma/login.html?error=true'
20:33:07,976 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
20:33:07,977 DEBUG [org.springframework.security.web.context.SecurityContextPersistenceFilter] - SecurityContextHolder now cleared, as request processing completed
20:33:07,988 DEBUG [org.springframework.security.web.util.AntPathRequestMatcher] - Checking match of request : '/login.html'; against '/resources/**'
20:33:07,988 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 1 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
20:33:07,988 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - HttpSession returned null object for SPRING_SECURITY_CONTEXT
20:33:07,988 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - No SecurityContext was available from the HttpSession: org.mortbay.jetty.servlet.HashSessionManager$Session:qvnxffio0znw@693465617. A new one will be created.
20:33:07,988 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 2 of 11 in additional filter chain; firing Filter: 'LogoutFilter'
20:33:07,988 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 3 of 11 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
20:33:07,988 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 4 of 11 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
20:33:07,989 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 5 of 11 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
20:33:07,989 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 6 of 11 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
20:33:07,989 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 7 of 11 in additional filter chain; firing Filter: 'RememberMeAuthenticationFilter'
20:33:07,989 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 8 of 11 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
20:33:07,989 DEBUG [org.springframework.security.web.authentication.AnonymousAuthenticationFilter] - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@90541710: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1%0; SessionId: qvnxffio0znw; Granted Authorities: ROLE_ANONYMOUS'
20:33:07,989 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 9 of 11 in additional filter chain; firing Filter: 'SessionManagementFilter'
20:33:07,989 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 10 of 11 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
20:33:07,989 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true at position 11 of 11 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
20:33:07,989 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - Secure object: FilterInvocation: URL: /login.html?error=true; Attributes: [permitAll]
20:33:07,989 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@90541710: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1%0; SessionId: qvnxffio0znw; Granted Authorities: ROLE_ANONYMOUS
20:33:07,989 DEBUG [org.springframework.security.access.vote.AffirmativeBased] - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@3a34c0bd, returned: 1
20:33:07,989 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - Authorization successful
20:33:07,989 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - RunAsManager did not change Authentication object
20:33:07,989 DEBUG [org.springframework.security.web.FilterChainProxy] - /login.html?error=true reached end of additional filter chain; proceeding with original chain
20:33:07,992 DEBUG [org.springframework.security.web.access.ExceptionTranslationFilter] - Chain processed normally
20:33:07,992 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
20:33:07,992 DEBUG [org.springframework.security.web.context.SecurityContextPersistenceFilter] - SecurityContextHolder now cleared, as request processing completed
答案 0 :(得分:1)
您的日志讲述了另一个故事 - UserDetailsService
被调用但引发了异常:
20:33:07,744 DEBUG [org.springframework.security.authentication.ProviderManager] - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider
20:33:07,968 WARN [org.springframework.security.authentication.event.LoggerListener] - Authentication event AuthenticationFailureServiceExceptionEvent: asdfasdfaasdf; details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffff8868: RemoteIpAddress: 0:0:0:0:0:0:0:1%0; SessionId: null; exception: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection
JDBC URL /凭据不正确?数据库没有运行?