首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring安全性:本地主页无法工作

Spring安全性:本地主页无法工作
EN

Stack Overflow用户
提问于 2016-03-26 21:44:08
回答 1查看 7K关注 0票数 1

web.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/root-context.xml
            /WEB-INF/spring/security-context.xml
        </param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

<!-- security config  -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

security-context.xml

  1. http://www.springframework.org/schema/beans/spring-beans.xsd [http://www.springframework.org/schema/security](http://www.springframework.org/schema/security) [http://www.springframework.org/schema/security/spring-security-4.0.xsd](http://www.springframework.org/schema/security/spring-security-4.0.xsd)"> ”/>:http><安全:用户服务><安全性:用户name="spider“password="peter”authorities="ROLE_USER"/> <安全:用户name="thor“password="thor”authorities="ROLE_USER"/>

login.jsp

代码语言:javascript
复制
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>login</title>
</head>
<body>
    <form method="POST">
        Name:<input type="text" name="uname"><br>
        Pass:<input type="password" name="pass"><br>
        <sec:csrfInput/>
        <input type="submit" value="Login">
    </form>
</body>
</html>

HomeController.java

代码语言:javascript
复制
@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 = "/home", 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 "home";
    }

    @RequestMapping(value="/login",method=RequestMethod.GET)
    public String goLogin(){
        return "login";
    }

}

**> **url是“localhost:8080/控制器/登录”*

和我得到的localhost页面无法工作,localhost重定向了您太多次。ERR_TOO_MANY_REDIRECTS

EN

回答 1

Stack Overflow用户

发布于 2016-03-26 23:06:00

ERR_TOO_MANY_REDIRECTS是一个迹象,表明您有一个重定向循环。在您的例子中,您尝试访问登录页面,但是<security:intercept-url pattern="/**" access="ROLE_USER"/>声明您需要被记录为每个USERUSER。Spring安全性然后尝试转发到登录URL,这将触发另一个重定向。

要解决问题,您需要为/login URL定义一个安全豁免,允许匿名用户查看登录页面。

祝好运,

问候丹尼尔

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36241248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档