Spring Security框架

Spring Security框架入门

1.1 Spring Security简介

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

 

2.配置文件

pom.xml

        <!--spring security相关  -->
        <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>

 

spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 为什么要用2个beans, -->
<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>
    
    <!-- 页面拦截规则 -->
    <http use-expressions="false">
        <!-- /** 表示根目录及以下子目录都在匹配范围,access="ROLE_USER"   配置角色名称,必须以ROLE_开头 -->    
         <!-- 当前用户必须有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"/>    <!-- 关闭csrf(跨站请求伪造) ,如果不加会出现错误 403-->
    </http>
    
    
    
    

    <!-- 认证管理器 -->
    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="admin" password="123456" authorities="ROLE_USER"/>
            </user-service>        
        </authentication-provider>    
    </authentication-manager>
    
</beans:beans>

 

posted @ 2020-12-26 22:30  登风360  阅读(109)  评论(0编辑  收藏  举报