当前位置: 首页 > 知识库问答 >
问题:

Spring Servlet和Spring Security过滤链Url模式

郭翰翮
2023-03-14

我很难在spring Security的j_spring_security_check和j_spring_security_logout上删除这些404条消息。我认为SpringServlet URL模式、springSecurityFilterChain URL模式和/或SiteMesh之间可能存在一些冲突。

这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

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

    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

和我的ApplicationContext(包括spring-security部分):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:security="http://www.springframework.org/schema/security"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <context:component-scan base-package="com.xpto.portal" />
    <tx:annotation-driven transaction-manager="txManager" />
    <mvc:annotation-driven />

    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
        <property name="order" value="1" />
    </bean>

    <security:http auto-config="true" use-expressions="true">
        <security:intercept-url pattern="/" access="hasRole('ROLE_USER')" />
        <security:intercept-url pattern="/logout" access="permitAll" />
        <security:logout logout-url="/logout"/> 
    </security:http>

    <!-- Declare an authentication-manager to use a custom userDetailsService -->
    <security:authentication-manager>
        <security:authentication-provider user-service-ref="userDetailsService">
            <security:password-encoder ref="passwordEncoder" />
        </security:authentication-provider>
    </security:authentication-manager>

    <bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder" />

    <security:user-service id="userDetailsService">
        <security:user name="john" password="21232f297a57a5a743894a0e4a801fc3" authorities="ROLE_USER, ROLE_ADMIN" />
        <security:user name="jane" password="ee11cbb19052e40b07aac0ca060c23ee" authorities="ROLE_USER" />
    </security:user-service>


    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages">
        </property>
        <property name="defaultEncoding" value="UTF-8">
        </property>
    </bean>

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang">
        </property>
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="pt">
        </property>
    </bean>
</beans>
@Controller
@RequestMapping(value = "/")
public class HomeController {

    @RequestMapping(method=RequestMethod.GET)
    public String index()
    {
        return "home/index";
    }
}
<a href="<c:url value="/logout" />">Logout</a>

它生成404和以下错误:

17:06:43,657警告[org.springframework.web.servlet.PageNotFound](默认任务-27)在名为“Spring Servlet”的DispatcherServlet中没有找到URI为[/portal/logout]的HTTP请求映射

这是相同的,不管我是否更改注销URL。

我使用的是spring-security 4.0.1

谢谢!

共有1个答案

柴嘉禧
2023-03-14

更改注销。像这样的东西。

<a href='<c:url value="/j_spring_security_logout" />'>
 类似资料:
  • 还有,如果真的要给出另一个模式,“/restful/**”不是更好吗?因为“/**”将匹配与“/RESTFUL/**”不匹配的URL,因此不由此筛选器链处理。

  • 假设我已经定义了这些RESTendpoint: 我定义了两个过滤器(日志记录和授权),在这些过滤器中,我想获得与当前请求匹配的url模式。使用上述示例: 如果请求是一个GET到 /variables,我需要"/变量" 如果请求是一个GET到 /variables/myfancyname,我需要"/变量/{name}" 如果请求是一个PUT到 /variables/myfancyname/myval

  • 主要内容:FilterChain 接口,Filter 链的拦截过程,Filter 链中 Filter 的执行顺序,示例在 Web 应用中,可以部署多个 Filter,若这些 Filter 都拦截同一目标资源,则它们就组成了一个 Filter 链(也称过滤器链)。过滤器链中的每个过滤器负责特定的操作和任务,客户端的请求在这些过滤器之间传递,直到传递给目标资源。 FilterChain 接口 javax.servlet 包中提供了一个 FilterChain 接口,该接口由容器实现。容器将其实例对象

  • 主要内容:实现,Person.java,Criteria.java,CriteriaMale.java,CriteriaFemale.java,CriteriaSingle.java,AndCriteria.java,OrCriteria.java,CriteriaPatternDemo.java过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型

  • 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。 实现 我们将创建一个 Person 对象、Criteria 接口和实现了该接口的实体类,来过滤 Person 对象的列表。CriteriaP

  • 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。 实现 我们将创建一个 Person 对象、Criteria 接口和实现了该接口的实体类,来过滤 Person 对象的列表。CriteriaP