Ich versuche Spring Security in meiner Anwendung zu implementieren und irgendwie habe ich ein Problem damit. Immer wenn ich auf eine der abgefangenen URLs stoße, erhalte ich eine benutzerdefinierte Anmeldeseite. Nach der erfolgreichen Anmeldung leitet meine Spring Security sie jedoch nicht an den Authentifizierungserfolgshandler weiter.Warum leitet meine Spring Security die Anfrage nicht an den Authentication-Success-Handler weiter?
Mein security-context.xml
ist:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/reviewer/**"
access="isAuthenticated()" requires-channel="any" />
<security:form-login login-page="/home"
authentication-failure-url="/authfailed?errormessage=authentication.login.failed"
authentication-success-handler-ref="successHandler"
/>
<security:logout logout-url="/logoutsuccess" logout-success-url="/logoutsuccess" />
<!-- <access-denied-handler ref="" error-page="/signup" /-->
</security:http>
<bean id="successHandler"
class="com.reviewthedoctors.security.WebAuthenticationSuccessHandler">
</bean>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider user-service-ref="userService">
</security:authentication-provider>
</security:authentication-manager>
<security:jdbc-user-service id="userService" data-source-ref="dataSource"
users-by-username-query=
"select email,password, true from users where email=?"
authorities-by-username-query=
"select email, authority from users where email =? " />
</beans>
Und meine WebAuthenticationSuccessHandler
Klasse:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.util.StringUtils;
public class WebAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
private RequestCache requestCache = new HttpSessionRequestCache();
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws ServletException, IOException {
SavedRequest savedRequest = requestCache.getRequest(request, response);
if (savedRequest == null) {
super.onAuthenticationSuccess(request, response, authentication);
return;
}
String targetUrlParameter = getTargetUrlParameter();
if (isAlwaysUseDefaultTargetUrl()
|| (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
requestCache.removeRequest(request, response);
super.onAuthenticationSuccess(request, response, authentication);
return;
}
clearAuthenticationAttributes(request);
String targetUrl = savedRequest.getRedirectUrl();
getRedirectStrategy().sendRedirect(request, response, targetUrl);
}
public void setRequestCache(RequestCache requestCache) {
this.requestCache = requestCache;
}
}
Ähnlich Anmeldungs Seite lautet:
<div class="page_content">
<div class="demo-card-wide mdl-card mdl-shadow--2dp"
style="margin-left: 50px; margin-top: 50px; width: 400px;">
<div class="mdl-card__title">
<h2 class="mdl-card__title-text">Login</h2>
</div>
<div class="mdl-card__supporting-text" style="width: 400px;">
<form action="/revewthemovies/j_spring_security_check" method="post" modelAttribute="user">
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" id="email"
name="j_username" modelAttribute="email" /> <label
class="mdl-textfield__label" for="name">Email</label>
</div>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="password" id="password"
name="j_password" modelAttribute="password" /> <label
class="mdl-textfield__label" for="name">Password</label>
</div>
<input type="submit"
class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent"
style="width: 150px; margin-bottom: 100px" value="Add" />
</form>
</div>
</div>
</div>
Ähnlich mein web.xml
ist:
<?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"
version="2.5">
<display-name>PRASM</display-name>
<!-- <welcome-file-list> <welcome-file>/pages/home.jsp</welcome-file> </welcome-file-list> -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>
Ich habe absolut keine Ahnung, warum die Anfrage WebAuthenticationSuccessHandler
nicht erreicht.
Jedes Mal, wenn ich mich einlogge in ich an dieser URL am Ende:
http://localhost:8080/myapp/j_spring_security_check
Hat jemand einen Vorschlag in Bezug auf diese?
von jan 28 2015 haben Sie 13 Fragen gestellt, aber Sie haben keine Antworten akzeptiert. Haben die Antworten deine Probleme nicht gelöst? – SpringLearner
Wir bedauern die Unannehmlichkeiten. Ich bin nicht so aktiv in Stackoverflow und wenn das ein Problem ist, werde ich definitiv Antworten für alle Fragen akzeptieren. Entschuldigung nochmal. – Smrita
@Smrita müssen Sie implementieren 'AuthenticationSuccessHandler'..WebAuthenticationSuccessHandler implementiert AuthenticationSuccessHandler' –