Srping security 入门

一、入门demo

1.新建maven web工程 

 

2.引入pom.xml依赖

<?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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.cmdzz</groupId>
  <artifactId>spring-security-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>spring-security-demo Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <spring.version>4.2.4.RELEASE</spring.version>
  </properties>

  <dependencies>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>4.1.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
      <version>4.1.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>


  </dependencies>
  <build>
    <plugins>
      <!-- java编译插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <!-- 指定端口 -->
          <port>9090</port>
          <!-- 请求路径 -->
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

3.引入web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">    

       <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-security.xml</param-value>
     </context-param>
     <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
     </listener>
    
     <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>

 

4.引入spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <!--页面拦截规则
        pattern="/*"  根目录下的资源 不包括子目录
        pattern="/**" 根目录下的资源 包括子目录
        角色:ROLE_XXX
        access="ROLE_USER" 表示当前用户必须有ROLE_USER的角色才可以访问根目录及其子目录
        use-expressions="false" 是否启用SPEL表达式 false为不启用 默认为true,
          如果不配置为false,access="hasRole(ROLE_USER)"
    -->
    <http use-expressions="false">
        <intercept-url pattern="/**" access="ROLE_USER"/>

        <!--开启表单登录功能  自动生成一个登录表单-->
        <form-login/>
    </http>

    <!--认证管理器-->
    <authentication-manager>
        <!--认证提供者-->
        <authentication-provider>
            <user-service>
                <!--配置当前系统的用户 这里手动添加用户名和密码
                    authorities="ROLE_USER"表示这个用户属于哪个角色
                -->
                <user name="admin" password="123456" authorities="ROLE_USER"/>
            </user-service>
        </authentication-provider>

    </authentication-manager>
        
</beans:beans>

 

5.编写index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
欢迎进入神奇的spring security世界
</body>
</html>

6.测试结果

地址输入:localhost:9090  其中/login是它自动跳转的

输入spring-security.xml  定义的用户名和密码  admin  123456

自动跳转到index.html

 

 二、自定义登录表单

1.编写自定义表单 login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
    <!--/login 是spring security自动生成的路径 必须要post提交-->
    <form action="/login" method="post">
        <!--spring security 默认接收表单登录为username password 修改要到配置文件修改-->
        用户名:<input name="username"><br>
        密码:<input name="password"><br>
        <button>登录</button>
    </form>
</body>
</html>

 

2.编写login_err.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陆失败</title>
</head>
<body>
    用户名或密码错误<br>
    登陆失败
</body>
</html>

 

3.修改spring-security.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <!--定义默认不拦截的资源
        设置页面不登陆也可以访问
    -->
    <http pattern="/login.html" security="none"></http>
    <http pattern="/login_err.html" security="none"></http>

    <!--页面拦截规则
        pattern="/*"  根目录下的资源 不包括子目录
        pattern="/**" 根目录下的资源 包括子目录
        角色:ROLE_XXX
        access="ROLE_USER" 表示当前用户必须有ROLE_USER的角色才可以访问根目录及其子目录
        use-expressions="false" 是否启用SPEL表达式 false为不启用 默认为true,
          如果不配置为false,access="hasRole(ROLE_USER)"
    -->
    <http use-expressions="false">
        <intercept-url pattern="/**" access="ROLE_USER"/>

        <!--开启表单登录功能  <form-login/>自动生成一个登录表单-->
        <!--<form-login/>-->
        <!--自定义登录表单配置
            login-page="/login.html"  登录表单
            default-target-url="/index.html"  登录成功后跳转到的页面
            authentication-failure-url="/login_err.html" 登录失败跳转的页面
        -->
        <form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_err.html"/>
    </http>

    <!--认证管理器-->
    <authentication-manager>
        <!--认证提供者-->
        <authentication-provider>
            <user-service>
                <!--配置当前系统的用户 这里手动添加用户名和密码
                    authorities="ROLE_USER"表示这个用户属于哪个角色
                -->
                <user name="admin" password="123456" authorities="ROLE_USER"/>
            </user-service>
        </authentication-provider>

    </authentication-manager>
        
</beans:beans>

 

 4.测试 输入localhost:9090

输入admin 和 123456 登录

发现出错了,原因是spring security 防止跨域请求,不允许静态页面访问,默认是jsp页面。可以到spirng-security-xml 关闭该配置。

<http use-expressions="false">
        <intercept-url pattern="/**" access="ROLE_USER"/>

        <!--开启表单登录功能  <form-login/>自动生成一个登录表单-->
        <!--<form-login/>-->
        <!--自定义登录表单配置
            login-page="/login.html"  登录表单
            default-target-url="/index.html"  登录成功后跳转到的页面
            authentication-failure-url="/login_err.html" 登录失败跳转的页面
        -->
        <form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_err.html"/>
        <!--关闭csrf跨域请求校验-->
        <csrf disabled="true"/>
    </http>

 

 再次测试 成功!

 

posted on 2019-06-21 00:23  成魔的蜘蛛  阅读(122)  评论(0)    收藏  举报

导航