【问题标题】:Spring Security issue - 404 errorSpring Security 问题 - 404 错误
【发布时间】:2016-07-05 10:28:54
【问题描述】:

我只是想执行一个非常简单的 Spring 安全示例项目,但我收到 404 错误。请帮助在这里找到问题。

project structure

控制器:

package mypack;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SecurityController {

@RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public ModelAndView welcomePage() {

    ModelAndView model = new ModelAndView();
    model.addObject("title", "Spring Security Hello World");
    model.addObject("message", "This is welcome page!");
    model.setViewName("hello");
    return model;

}

@RequestMapping(value = "/admin**", method = RequestMethod.GET)
public ModelAndView adminPage() {

    ModelAndView model = new ModelAndView();
    model.addObject("title", "Spring Security Hello World");
    model.addObject("message", "This is protected page!");
    model.setViewName("admin");

    return model;

}

}

spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="mypack" />

<bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
    <value>/WEB-INF/views/</value>
  </property>
  <property name="suffix">
    <value>.jsp</value>
  </property>
</bean>

spring-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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.2.xsd">

<http auto-config="true">
    <intercept-url pattern="/admin**" access="ROLE_USER" />
</http>

<authentication-manager>
  <authentication-provider>
    <user-service>
    <user name="user" password="123456" authorities="ROLE_USER" />
    </user-service>
  </authentication-provider>
</authentication-manager>

web.xml

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<!-- Spring MVC -->
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

    <!-- Loads Spring Security config file -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>

<!-- Spring Security -->
<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>

admin.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page session="true"%>
<html>
<body>
<h1>Title : ${title}</h1>
<h1>Message : ${message}</h1>

<c:if test="${pageContext.request.userPrincipal.name != null}">
   <h2>Welcome : ${pageContext.request.userPrincipal.name} 
       | <a href="<c:url value="/j_spring_security_logout" />" > Logout</a></h2>  
</c:if>

你好.jsp

<%@page session="false"%>
<html>
<body>
<h1>Title : ${title}</h1>   
<h1>Message : ${message}</h1>   

【问题讨论】:

  • 组件扫描为控制器扫描注解组件将解决问题
  • 存在于 spring-servlet.xml 中。抱歉,它在代码 sn-p 中不可见。现在编辑了。
  • 我在包含 spring 安全配置后开始收到 404 错误。如果我评论 web.xml 中存在的 spring 安全配置,项目工作正常。请注意,我没有包括登录页面。我期待 spring 提供的默认登录页面。

标签: java spring spring-mvc spring-security


【解决方案1】:

在您的 spring security xml 文件中尝试以下代码。还可以根据您的要求更改角色。

<http auto-config="true" use-expressions="true">
        <intercept-url pattern="/signin" access="permitAll"></intercept-url>
        <intercept-url pattern="/logout" access="permitAll"></intercept-url>
        <intercept-url pattern="/accessdenied" access="permitAll"></intercept-url>          
        <intercept-url method="GET"   pattern="/**"
            access="hasRole('USER') 
            or hasRole('ADMIN')"></intercept-url>   

        <form-login login-page="/signin" default-target-url="/index"
            authentication-failure-url="/accessdenied" always-use-default-target="true"
            username-parameter="username" password-parameter="password"></form-login>
        <logout logout-success-url="/logout"></logout>
    </http>

【讨论】:

    【解决方案2】:

    由于您已在 web.xml 中将 servlet 映射配置为 url-pattern 作为 servlet-mapping 中的“/”。因此调度程序 servlet 将寻找名为 index.html/jsp 的文件。

    要配置您的控制器,您必须将组件扫描添加到您的 servlet.xml

    添加下面一行

    <context:component-scan base-package="mypack" />
    

    【讨论】:

    • 存在于 spring-servlet.xml 中。抱歉,它在代码 sn-p 中不可见。现在编辑了。
    【解决方案3】:

    您必须为路径定义过滤器:

    <intercept-url pattern="/" access="permitAll" />
    <intercept-url pattern="/welcome" access="permitAll" />
    

    因此在这种情况下,没有角色(匿名)的用户可以访问“/”和“/welcome**”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-10
      • 2014-04-06
      • 2015-06-13
      • 2020-04-10
      • 2016-07-23
      • 2019-05-04
      • 1970-01-01
      • 2013-10-14
      相关资源
      最近更新 更多