SpringSecurity快速入门

作者:SingleXu
链接:https://www.jianshu.com/p/8212a559d633
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1 简介
Spring Security是Spring生态系统中的一员,提供安全机制的(软件的安全、程序的安全,非硬件安全)

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。

就帮我们解决两个问题

认证:验证用户名和密码是否合法(是否系统中用户),这是第一关;
授权:是系统用户不代表你能使用某些功能,因为你可能没有权限,这是第二关;
Spring Security底层使用的是过滤器,针对url进行的拦截,对应到java中也就是类; 因此被称为粗粒度授权验证,就是验证url,你当前用户有没有这个url的权限。

2 快速入门
省略父工程创建步骤,见:https://www.jianshu.com/p/8853bc384758

2.1 创建web项目
6.png
7.png
2.2 导入依赖
图片.png
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mytest_parent</artifactId>
        <groupId>com.mytest</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../mytest_parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring_security_01</artifactId>
    
    <packaging>war</packaging>
    <build>
        <plugins>
            <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>80</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
      

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>

        <!-- JSP相关 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

</project>
2.3 配置applicationContext_security.xml, 配置认证和授权信息
图片.png
<?xml version="1.0" encoding="UTF-8"?>
<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
">
    <!--
        配置拦截的规则
        auto-config="使用SpringSecurity自带的页面"
        use-expressions="是否使用spel表达式",如果使用表达式:hasRole('ROLE_USER')
    -->
    <security:http auto-config="true" use-expressions="false">
        <!--intercept-url 表示拦截页面   
          /*  表示的是该目录下的资源,只包括本级目录不包括下级目录
          /** 表示的是该目录以及该目录下所有级别子目录的资源
        配置拦截的请求地址,任何请求地址都必须有ROLE_USER的权限 -->
        <security:intercept-url pattern="/**" access="ROLE_USER"/>
    </security:http>

    <!-- 配置认证信息 -->
    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <!--noop是一个标识,表示密码不能明文,密码往往会有很多其他方式,这里默认编码就是不编码-->
                <security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>
2.4 配置过滤器
(这里的web.xml和index.jsp文件是手动创建的,名称路径一致即可)


图片.png
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--Spring监听器指定配置文件位置-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext_security.xml</param-value>
  </context-param>

  <!--配置委派代理过滤器-->
  <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>

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

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
2.5测试
打开tomcat插件测试
图片.png
访问localhost/index.jsp页面,如果当前用户没有登录认证的话,则跳转到SpringSecurity的内置登录页面


图片.png
输入配置文件中配置好的账户名admin密码admin,会出现如下404


图片.png

配置文件中增加,对网页缩略图的不拦截后,重启tomcat


图片.png
 <security:http pattern="/favicon.ico" security="none"/>
即可访问到index.jsp页面


图片.png
3 配置自定义的登录页面
图片.png
3.1 登录页面login.jsp
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
    <title>xxx系统登录页</title>

    <style>
        div{
            300px;
            height:100px;
            position: absolute;
            top:50%;
            left:50%;
            margin-top:-50px;
            margin-left:-150px;
        }
    </style>
</head>
<body>
<div>
    <form method="post" action="${pageContext.request.contextPath}/login">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="username"/></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password"/></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit"/></td>
            </tr>
        </table>
    </form>
</div>

</body>
</html>
3.2 成功页面: success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
登录成功,欢迎!
</body>
</html>
3.3 失败页面: error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
登录失败!!!
</body>
</html>
3.4 配置
图片.png

图片.png

图片.png
<?xml version="1.0" encoding="UTF-8"?>
<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="/login.jsp" security="none"/>
    <security:http pattern="/error.jsp" security="none"/>
    <security:http pattern="/favicon.ico" security="none"/>

    <!--===================授权验证: 验证角色的访问权限 ===================-->
    <!--
        配置拦截的规则
        auto-config="使用SpringSecurity自带的页面"
        use-expressions="是否使用spel表达式",如果使用表达式:hasRole('ROLE_USER')
    -->
    <security:http auto-config="true" use-expressions="false">
        <!-- 配置拦截的请求地址,任何请求地址都必须有ROLE_USER的权限 -->
        <security:intercept-url pattern="/**" access="ROLE_USER"/>
        <!--配置自定义登录相关页面-->
        <security:form-login
                login-page="/login.jsp"
                login-processing-url="/login"
                default-target-url="/success.jsp"
                authentication-failure-url="/error.jsp"
                username-parameter="username"
                password-parameter="password"
        />
        <!--关闭跨站请求伪造-->
        <security:csrf disabled="true"/>
    </security:http>

    <!--=========================授权认证: 校验用户名和密码的合法性====================-->
    <!-- 配置认证信息 -->
    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <!--noop是一个标识,表示密码不能明文,密码往往会有很多其他方式,这里默认编码就是不编码-->
                <security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>
3.5 测试1: 访问登录页面(localhost/login.jsp) 且 用户名和密码正确
http://localhost/login.jsp
图片.png

图片.png
3.6 测试2:访问localhost/index.jsp, 用户名和密码正确:
关闭浏览器重新访问
http://localhost/index.jsp

图片.png

图片.png
3.7 测试3: 访问index.jsp, 用户名或密码错误
http://localhost/index.jsp

图片.png
图片.png
6人点赞
日记本
"

作者:SingleXu
链接:https://www.jianshu.com/p/8212a559d633
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/Jeely/p/11943676.html