• Spring Security:入门


    依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-config</artifactId>
                <version>5.1.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-taglibs</artifactId>
                <version>5.1.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.1.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.26</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.1.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.1</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.1</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.8</version>
            </dependency>
            <dependency>
                <groupId>javax.annotation</groupId>
                <artifactId>jsr250-api</artifactId>
                <version>1.0</version>
            </dependency>
        </dependencies>
    

    web.xml

    <web-app 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_3_0.xsd"
             version="3.0">
    	<display-name>Archetype Created Web Application</display-name>
    
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
    
        <!--springsecurity核心过滤器链-->
        <!--filter-name:springSecurityFilterChain不能修改-->
        <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>
    

    spring security配置

    基本配置

    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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:security="http://www.springframework.org/schema/security"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    			    http://www.springframework.org/schema/beans/spring-beans.xsd
    			    http://www.springframework.org/schema/context
    			    http://www.springframework.org/schema/context/spring-context.xsd
    			    http://www.springframework.org/schema/aop
    			    http://www.springframework.org/schema/aop/spring-aop.xsd
    			    http://www.springframework.org/schema/tx
    			    http://www.springframework.org/schema/tx/spring-tx.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="/plugins/**" security="none"/>
        
        <!--
        auto-config:表示是否自动加载springSecurity的配置文件
        use-expressions 表示是否使用spring的el表达式来配置springSecurity
        -->
        <security:http auto-config="true" use-expressions="true">
            <!--拦截资源-->
            <!--
            access="hasAnyRole('ROLE_USER') 表示只有ROLE_USER角色才能访问资源
            -->
            <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER')"/>
        </security:http>
    
        <!--设置springSecurity的认证用户信息的来源-->
        <security:authentication-manager>
            <security:authentication-provider>
                <security:user-service>
                    <!--
                    暂时保存在内存中,两个用户
                     {noop}表示不加密认证
                    -->
                    <security:user name="user" password="{noop}user" authorities="ROLE_USER"/>
                    <security:user name="admin" password="{noop}admin" authorities="ROLE_ADMIN"/>
                </security:user-service>
            </security:authentication-provider>
        </security:authentication-manager>
    </beans>
    

    启动项目后,会发现自动跳转到springsecurity默认的登录页:

    image-20200930110824981

    我们可以配置自己的登录页和首页以及认证失败跳转页:

        <security:http auto-config="true" use-expressions="true">
            <!--认证页面可以匿名访问-->
            <security:intercept-url pattern="/login.jsp" access="permitAll()"/>
            <!--拦截资源-->
            <!--
            access="hasAnyRole('ROLE_USER') 表示只有ROLE_USER角色才能访问资源
            -->
            <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER')"/>
            <security:form-login login-page="/login.jsp"
                                 login-processing-url="/login"
                                 default-target-url="/index.jsp"
                                 authentication-failure-url="/failer.jsp"/>
            <!--配置退出登录信息-->
            <security:logout logout-url="/logout" logout-success-url="/login.jsp"/>
        </security:http>
    

    关闭csrf拦截

    spring security中,get请求默认不会被csrf拦截!!!

    配置完成后,登陆,发现被csrf拦截了:

    image-20200930112211455

    我们可以关闭csrf拦截:

            <!--去掉csrf拦截-->
            <security:csrf disabled="true"/>
    

    开启csrf拦截

            <!--去掉csrf拦截-->
            <security:csrf disabled="false"/>
    

    jsp中加入:

    <%@taglib uri="http://www.springframework.org/security/tags" prefix="security"%>
    

    并且在form登陆表单中,加入以下标签:

    <security:csrfInput/><%--跟form一起用的--%>
    <%--ajax请求使用<security:csrfMetaTags/>这个--%>
    

    即可登陆成功

    退出登陆功能

    xml配置

    <!--配置退出登录信息-->
    <security:logout logout-url="/logout" logout-success-url="/login.jsp"/>
    

    注意,一旦开启csrf拦截功能后,退出登陆必须用form表单发送post请求

    <form action="${pageContext.request.contextPath}/logout" method="post">
    	<security:csrfInput/>
    	<input type="submit" value="注销">
    </form>
    
  • 相关阅读:
    ArcGIS Pro获得工作目录和设置工作目录
    文献可视化神器—VOSviewer
    为hade增加model自动生成功能
    如何封装安全的go
    极其详尽的OpenWRT软路由系统安装与设置教程(物理机安装)
    资本运作模式(融资与投资) 运营管理文章
    软路由的路由功能设置 初学者系列 学习者系列文章
    安装kali linux操作系统(转) 初学者系列 学习者系列文章
    USBC数据线改名了!不提版本、只看速度
    安装Windows11操作系统(不需要绕过TPM检测脚本等) 初学者系列 学习者系列文章
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/13754448.html
Copyright © 2020-2023  润新知