【问题标题】:This webpage has a redirect loop in spring-security application此网页在 spring-security 应用程序中有一个重定向循环
【发布时间】:2013-03-07 13:29:14
【问题描述】:

我在 Spring 中有一个使用 Spring Security 的 Web 应用程序,当我尝试执行它说的应用程序时

This webpage has a redirect loop

这是我的security-context.xml,添加后只有我得到这个异常

<?xml version="1.0" encoding="UTF-8"?>

<b:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <!-- HTTP security configurations -->
    <http use-expressions="true">
        <form-login login-processing-url="/resources/j_spring_security_check"
            login-page="/login" authentication-failure-url="/login?login_error=t" />
        <logout logout-url="/resources/j_spring_security_logout" />
        <intercept-url pattern="/**" access="isAuthenticated()" />
        <intercept-url pattern="/login*" access="permitAll()" />
        <intercept-url pattern="/resources/**" access="permitAll()" />

    </http>

    <!-- Configure Authentication mechanism -->
    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="admin" password="admin" authorities="RIGHT_LIST,RIGHT_CANCEL,RIGHT_CREATE,RIGHT_UPDATE" />
                <user name="antony" password="antony" authorities="RIGHT_LIST,RIGHT_CANCEL,RIGHT_CREATE,RIGHT_UPDATE" />
                <user name="rod" password="rod" authorities="RIGHT_LIST,RIGHT_CREATE"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>

    <global-method-security secured-annotations="enabled" pre-post-annotations="enabled">
        <expression-handler ref="expHandler"/>
    </global-method-security>

    <b:bean id="expHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
        <b:property name="permissionEvaluator">
            <b:bean  class="com.anto.springsec.security.CreateContactPermissionEvaluator"/>
        </b:property>
    </b:bean>

</b:beans>

我有一个 login.jsp 和一个 createContact.jsp

这是我的家庭控制器:

package com.anto.springsec.controllers;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );

        return "login";
    }

}

请帮我解决这个问题。

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    我相信intercept-url 的顺序在这里很重要,而且您的/** 模式似乎也在吞噬/login/resources

    试试这个:-

    <http pattern="/resources/**" security="none"/>
    <http pattern="/login" security="none"/>
    
    <http use-expressions="true">
        <form-login login-processing-url="/resources/j_spring_security_check"
            login-page="/login" authentication-failure-url="/login?login_error=t" />
        <logout logout-url="/resources/j_spring_security_logout" />
        <intercept-url pattern="/**" access="isAuthenticated()" />
    </http>
    

    此配置与我现有的一个项目非常相似。

    更新

    这是我目前在项目中使用的配置:-

    <security:http pattern="/resources/**" security="none"/>
    <security:http pattern="/login" security="none"/>
    <security:http pattern="/error/**" security="none"/>
    
    <security:http auto-config="true">
        <security:form-login login-page="/login"
                             authentication-failure-url="/login?login_error=1"
                             default-target-url="/"
                             always-use-default-target="true"/>
        <security:logout logout-success-url="/"/>
        <security:intercept-url pattern="/**" access="ROLE_USER"/>
    </security:http>
    

    03-19-13

    为了在http标签中使用security属性,你需要Spring Security 3.1...见http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#new-3.1-ns

    【讨论】:

    • 使用您的配置设置时出现异常
    • 我用我的项目中的实际配置更新了我上面的帖子。
    • 在此行找到多个注释:- cvc-complex-type.3.2.2:属性“安全”不允许出现在元素“http”中。 - cvc-complex-type.2.4.a:发现以元素“http”开头的无效内容。之一
    • 看来您使用的是 Spring Security 3.0,如果有区别的话,我使用的是 3.1。
    • 这绝对是 3.1 的东西,请参阅 static.springsource.org/spring-security/site/docs/3.1.x/… 的第 3.2.3 节 ...我的配置是有效的。
    【解决方案2】:

    试试这个

    改变

    &lt;intercept-url pattern="/login*" access="permitAll()" /&gt;

    <intercept-url pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 
    

    <intercept-url pattern="/login*" access="isAnonymous()" />
    

    因为你有表达真我认为你必须使用 isAnonymous()

    See docs

    intercept-url 元素表示对登录页面的任何请求都应该可供匿名用户使用。否则请求将被模式 /** 匹配,并且无法访问登录页面本身!这是一个常见的配置错误,会导致应用程序出现无限循环。 Read more from here

    【讨论】:

      猜你喜欢
      • 2014-01-11
      • 1970-01-01
      • 2012-09-22
      • 2015-12-02
      • 2014-11-21
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多