2013-03-27 77 views
4

我有一個Spring-MVC應用程序,我希望在多個頁面上顯示一個登錄欄 - 並使用jQuery的對話框系統在模態對話窗口中顯示窗體。我應該在securityContext.xml中使用什麼樣的Spring-Security設置才能工作?Spring-security如何從每個頁面登錄?

這是我目前使用:

<http pattern="/resources/**" security="none" /> 

<http auto-config="true" use-expressions="true"> 
    <intercept-url pattern="/redirect.jsp" access="permitAll" /> 
    <intercept-url pattern="/*.html" access="permitAll" /> 
    <intercept-url pattern="/**" access="denyAll" />   
    <form-login login-page="" /> 
    <logout logout-success-url="/logout" /> 
</http> 

回答

2

登錄是說,應用程序的另一種方式沒有任何入口點。所以我們需要配置一個什麼都不做的入口點。

我們可以如下做到這一點:

public class DoNothingEntryPoint implements AuthenticationEntryPoint { 

    @Override 
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) 
     throws IOException, ServletException { 

     /* do nothing */ 

     } 
} 


in XML: 
<beans:bean id="doNothingEntryPoint" class="xyz.package.DoNothingEntryPoint" /> 

<http entry-point-ref="doNothingEntryPoint" use-expressions="true"> 
.... 
+0

感謝,確實做到了。 – 2013-03-28 10:31:31

0

如果您使用JSP,你可以把春天標籤的優勢:從每一個頁面

<sec:authorize ifNotGranted="ROLE_ANONYMOUS"> 
//User is loggedin 
       Welcome, ${pageContext.request.userPrincipal.principal.nameToDisplay} 
       <a href="j_spring_security_logout">Logout</a> 
       </sec:authorize> 
<sec:authorize access="isAnonymous()"> 
//user is not loggedin, show login form 
       <form id="loginForm" onsubmit="DoLoginInline();" target="passwordIframe" > 
       <label>Login:</label> 
       <input type="text" class="headerInputs" id="LoginUsername" name="j_username"> 
       <label>Password:</label> 
       <input type="password" class="headerInputs" id="LoginPassword" name="j_password"> 


       <button type="submit" value="Submit"></button> 

       </form> 

      </sec:authorize>