Spring Security 框架(安全框架)

Spring Security 简介

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

项目搭建流程

创建maven项目

配置pom.xml


    4.2.4.RELEASE




    
        org.springframework
        spring-core
        ${spring.version}
    
    
        org.springframework
        spring-web
        ${spring.version}
    

    
        org.springframework
        spring-webmvc
        ${spring.version}
    

    
        org.springframework
        spring-context-support
        ${spring.version}
    

    
        org.springframework
        spring-test
        ${spring.version}
    

    
        org.springframework
        spring-jdbc
        ${spring.version}
    

    
    
        org.springframework.security
        spring-security-web
        4.1.0.RELEASE
    

    
        org.springframework.security
        spring-security-config
        4.1.0.RELEASE
    

    
        javax.servlet
        servlet-api
        2.5
        provided
    




       
      
      
            org.apache.maven.plugins
            maven-compiler-plugin
            3.2
            
                1.7
                1.7
                UTF-8
            
            
      
            org.apache.tomcat.maven
            tomcat7-maven-plugin
            
                
                9090
                
                /
            
      
     

配置web.xml文件

 
    contextConfigLocation
    classpath:spring-security.xml
 
 
    
         org.springframework.web.context.ContextLoaderListener
          
 

 
 
    springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
 

    springSecurityFilterChain
    /*
 

配置Spring Security.xml中的注意

Spring Security 框架(安全框架)_第1张图片
Snip20170920_2.png

注意前缀因为在当前xml中配置文件存在两个头,需要前缀区分,

Spring Security 框架(安全框架)_第2张图片
Snip20170920_8.png



    
    
    
    



    
    
        
            
            
        
    

添加测试文件在webapp文件夹中添加index.html

Spring Security 框架(安全框架)_第3张图片
Snip20170920_9.png

报错404:在第一次登录的时候可能会出现404,没关系刷新HTML然后就可以了

上面是spring security中自带登录界面,下面自定义一个登录界面

创建测试html

Spring Security 框架(安全框架)_第4张图片
Snip2017092.png

在login.html中输入

Spring Security 框架(安全框架)_第5张图片
Snip20170920_14.png

在spring-security.xml中修改form-login

Snip20170920_12.png

注意一定要加下面的HTML设置那些页面可以直接访问,不用加密

然后测试就可以了

你可能感兴趣的:(Spring Security 框架(安全框架))