2013-03-19 61 views
12

我在春天有一个Web应用程序中的重定向循环,它使用Spring Security的,当我尝试EXCUTE应用它说该网页在弹簧安全应用

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

这是我家CONTROLER:

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

} 

请帮我解决这个问题。

回答

13

我相信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> 

此配置非常类似于我现有的项目之一。

UPDATE

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

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

13年3月19日

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

+0

当我使用你的配置设置时,我得到了异常 – 2013-03-19 02:00:29

+0

我用我的项目的实际配置更新了我的帖子。 – limc 2013-03-19 02:01:53

+0

在该行发现多个注释: \t - cvc-complex-type.3.2.2:属性'security'不允许出现在元素'http'中。 \t - cvc-complex-type.2.4.a:找到以元素“http”开头的无效内容。 – 2013-03-19 02:02:25

2

试试这个

变化

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

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

OR

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

当你有表达真我觉得哟u必须使用isAnonymous()

See docs

拦截的URL元说,对于登录页面的任何请求应提供给匿名用户。否则,请求将被模式/ **匹配,并且不可能访问登录页面本身!这是一个常见的配置错误,并且会导致应用程序中出现无限循环。 Read more from here