zoukankan      html  css  js  c++  java
  • springSecurity入门小demo--配置文件xml的方式

    本例子只是一个最最最简单的入门demo,重点讲解xml的配置参数的意思和遇到的坑,主要的功能有:

    • 自定义登录页面,错误页面
    • 配置角色
    • csrf-403报错解决方法(加上一行代码配置就ok)
    • 后台iframe框架页面不显示问题解决
    • 登出功能

    pom.xml

    <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>cn.itcast.demo</groupId>
      <artifactId>spring-security-demo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      
      <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>
    View Code

    index.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>首页</title>
    </head>
    <body>
    欢迎进入神奇的spring security世界~~~
    </body>
    </html>
    View Code

    login.html   (这个有坑,字段必须是username和password,必须post提交,必须接口是/login)

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>登陆</title>
    </head>
    <body>
    
    --欢迎的登陆我的系统--
    <form action="/login" method="post">
        用户名:<input name="username"><br>
        密码:<input name="password"><br>
        <button>登陆</button>
    </form>
    
    
    </body>
    </html>
    View Code

    login_error.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>首页</title>
    </head>
    <body>
    用户名或密码错误~~~
    </body>
    </html>
    View Code

    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_error.html" security="none"></http>
    
        <!-- 页面的拦截规则    use-expressions:是否启动SPEL表达式 默认是true -->
        <http use-expressions="false">
            <!-- 当前用户必须有ROLE_USER的角色 才可以访问根目录及所属子目录的资源 -->
            <intercept-url pattern="/**" access="ROLE_USER"/>
            <!-- 开启表单登陆功能 -->
            <form-login   login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>
            <csrf disabled="true"/>
            <!-- 如果是后台框架,一般都是用iframe框架嵌套的,security默认不支持这种,需要加上这个参数,页面才会正确显示出来 -->
            <headers>
                <frame-options policy="SAMEORIGIN"/>
            </headers>
            <!-- 登出添加这一行代码就ok -->
            <logout/>
        </http>
        
        <!-- 认证管理器 -->
        <authentication-manager>
            <authentication-provider>
                <user-service>
                    <user name="admin" password="123456" authorities="ROLE_USER"/>
                </user-service>
            </authentication-provider>    
        </authentication-manager>
            
    </beans:beans>
    View Code

    注意:use-expressions默认是true,如果我们不关闭的话,那么我们下面自定义访问权限就必须这样写<intercept-url pattern="/**" access="hasRole('ROLE_USER')"/> 很麻烦

    如果出现csrf错误的话,就加上这句话<csrf disabled="true"/>

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

    注意:springSecurityFilterChain必须这个名字。

  • 相关阅读:
    在Eclipse中运行JAVA代码远程操作HBase的示例
    hbase基本概念和hbase shell常用命令用法
    如何使用putty远程连接linux
    如何在Eclipse下安装SVN插件——subclipse
    solr之创建core(搜索核心,包括索引和数据)的方法
    百度地图api基本用法
    四年大学不如选择培训一年?
    树常见的算法操作
    二叉树常见遍历算法
    Java多线程实现生产者消费者延伸问题
  • 原文地址:https://www.cnblogs.com/coder-lzh/p/9103131.html
Copyright © 2011-2022 走看看