Spring-security如何从每个页面登录?

时间:2013-03-27 12:36:37

标签: spring-security

我有一个Spring-MVC应用程序,我希望在多个页面上显示一个登录栏 - 并使用jQuery的对话框系统在模式对话框窗口中显示表单。我应该在securityContext.xml中使用什么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 个答案:

答案 0 :(得分: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">
....

答案 1 :(得分:0)

如果你使用jsp,你可以利用spring标签:

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