2013-03-27 76 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>