springSecurity配置过滤静态文件

1.添加依赖

<properties>
    <spring.version>5.0.2.RELEASEspring.version>
    <spring.security.version>5.0.1.RELEASEspring.security.version>
  properties>
  <dependencies>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframework.securitygroupId>
      <artifactId>spring-security-webartifactId>
      <version>${spring.security.version}version>
    dependency>
    <dependency>
      <groupId>org.springframework.securitygroupId>
      <artifactId>spring-security-configartifactId>
      <version>${spring.security.version}version>
    dependency>
    <dependency>
      <groupId>org.springframework.securitygroupId>
      <artifactId>spring-security-taglibsartifactId>
      <version>${spring.security.version}version>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
      <scope>providedscope>
    dependency>
  dependencies>

2.创建spring-security的xml文件


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

    
    <security:http pattern="/css/**" security="none"/>
    <security:http pattern="/img/**" security="none"/>

    
    <security:http pattern="/login.jsp" security="none"/>
    <security:http pattern="/error.jsp" security="none"/>

    
    <security:http auto-config="true" use-expressions="false">
        
        <security:intercept-url pattern="/**" access="ROLE_USER"/>

        
        <security:form-login
                login-page="/login.jsp"
                login-processing-url="/login"
                always-use-default-target="true"
                default-target-url="/success.jsp"
                authentication-failure-url="/error.jsp"
        />

        
        <security:csrf disabled="true"/>
        
        <security:logout invalidate-session="true"
                         logout-url="/logout"
                         logout-success-url="/login.html"/>
    security:http>

    
    <security:authentication-manager>
        
        <security:authentication-provider>
            
            <security:user-service>
                
                
                <security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
            security:user-service>
        security:authentication-provider>
    security:authentication-manager>
beans>

3.web.xml配置


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

  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring-security.xmlparam-value>
  context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>

  
  <filter>
    <filter-name>springSecurityFilterChainfilter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
  filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChainfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

web-app>

4.运行时即可看到相应的配置界面

springSecurity配置过滤静态文件_第1张图片

你可能感兴趣的:(spring,springSecurity)