2016-09-24 17 views
-2

私は自分のアプリケーションにSpring Securityを実装しようとしていますが、何とか問題があります。インターセプトされたURLの1つを押すと、カスタムログインページが表示されます。しかし、ログインが成功した後、私のSpring Securityはそれを認証成功ハンドラに転送しません。私の春のセキュリティは、認証成功ハンドラに要求を転送しないのはなぜですか?

マイsecurity-context.xmlは次のとおりです。

<?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>  

そして、私のWebAuthenticationSuccessHandlerクラスは次のとおりです。

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

同様に、私のログインページがある:

<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> 

は同様に私の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" 
    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> 

なぜ、リクエストがWebAuthenticationSuccessHandlerに届かないのかわかりません。

私は私にログインするたびには、次のURLで終わる:

http://localhost:8080/myapp/j_spring_security_check 

誰もがこれについてどんな提案を持っていますか?

+2

jan 28 2015から、あなたは13の質問をしましたが、回答を受け入れていません。答えがあなたの問題を解決しなかったのですか? – SpringLearner

+0

ご迷惑をおかけして申し訳ありません。私はStackoverflowでそれほど活発ではないし、それが問題なら、私はすべての質問に対して確実に答えを受け入れるだろう。またすみません。 – Smrita

+0

@Smritaあなたは 'AuthenticationSuccessHandler'を実装する必要があります.'WebAuthenticationSuccessHandlerはAuthenticationSuccessHandler'を実装しています –

答えて

2

問題は、web.xmlで次のように宣言するのを忘れたことです。

<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> 
関連する問題