zoukankan      html  css  js  c++  java
  • spring内置安全框架 Spring Security

    1.1 Spring Security简介

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

    1.2pom.xml ,添加依赖

    复制代码
          <!-- 身份验证 -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>        
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>        
        </dependency>
    复制代码

    1.3修改web.xml ,增加SpringSecurity过滤器

    复制代码
     <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>    
    复制代码

    1.4spring目录下添加配置文件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 pattern="/css/**" security="none"/>
        <http pattern="/img/**" security="none"/>
        <http pattern="/js/**" security="none"/>
        <http pattern="/plugins/**" security="none"/>
        
        <!-- use-expressions:设置是否启动SpEL表达式,默认值是true。 -->
        <http use-expressions="false">
            <!-- 
                配置SpringSecurity的拦截路径(拦截规则) 
                * pattern:配置拦截规则。   /* 代表的是根路径下的所有资源(不包含子路径) /**代表的是根路径下所有的资源(包含子路径)
                * access:设置角色  角色命名 ROLE_角色名称  如:  ROLE_USER  
            -->
            <intercept-url pattern="/**" access="ROLE_ADMIN"/>
            
            <!-- 
            开启表单验证 
                username-parameter="username" 
                password-parameter="password" 
                login-page            :登录页面名称  以  / 开始
                default-target-url    :登录成功后跳转的页面
                login-processing-url:提交的路径的设置 默认值"/login" 可以修改
            -->
            <form-login login-page="/login.html" default-target-url="/admin/index.html" always-use-default-target="true" authentication-failure-url="/login.html"/>
            
            <!-- 不使用csrf的校验 -->
            <csrf disabled="true"/>
            
            <!-- 配置框架页面不拦截 -->
            <headers>
                <frame-options policy="SAMEORIGIN"/>
            </headers>
            
            <!-- 注销的配置 -->
            <logout logout-url="/logout" logout-success-url="/login.html" />
        </http>
        
        <!-- 配置认证管理器 -->
        <authentication-manager>
            <!-- 认证的提供者 -->
            <authentication-provider>
                <user-service>
                    <user name="admin" password="123456" authorities="ROLE_ADMIN"/>
                    <user name="wc" password="123456" authorities="ROLE_ADMIN"/>
                </user-service>
            </authentication-provider>
        </authentication-manager>
            
    </beans:beans>
    复制代码

     配置说明:

    always-use-default-target:指定了是否在身份验证通过后总是跳转到default-target-url属性指定的URL。

    如果你在系统中使用了框架页,需要设置框架页的策略为SAMEORIGIN

    <headers>
        <frame-options policy="SAMEORIGIN"/>
    </headers>

    SAMEORIGIN:frame页面的地址只能为同源域名下的页面

    注意:该spring安全框架登陆显示登陆信息需要自己手写接口

    复制代码
    package cn.xhn.manager.controller;
    
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @RequestMapping("/login")
    @RestController
    public class LoginController {
    
        //实现登陆功能
        @RequestMapping("/showName")
        public Map login(){
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            Map map=new HashMap();
            map.put("username",username);
            return map;
        }
    }
    复制代码

    登出功能自动点击即可

  • 相关阅读:
    Nginx会话保持之nginx-sticky-module模块
    企业级分布式应用服务EDAS _Dubbo商业版_微服务PaaS平台 【EDAS Serverless 运维 创业】
    git repository description
    运维成长
    jenkins+maven+tomcat集群发布
    Leaf——美团点评分布式ID生成系统 UUID & 类snowflake
    tomcat redis 集群 session共享
    JEECG & JEESite Tomcat集群 Session共享
    分布式Tomcat session会话Sticky Sessions问题
    Memcached 集群架构与memcached-session-manager
  • 原文地址:https://www.cnblogs.com/wufeng6/p/11921427.html
Copyright © 2011-2022 走看看