zoukankan      html  css  js  c++  java
  • springSecurity安全框架

    一、是什么

    是一种基于 Spring AOP 和 Servlet 过滤器的安全框架,对访问权限进行控制

    二、作用

    1.认证

      用户名和密码认证,核对是否正确

    2.授权

      若正确,给予登录用户对应的访问权限

    3.攻击防护

    三、注意事项

    1.登录页面提交用户名、密码表单路径必须是 /login

     

     

    2.登录页面用户名和密码输入框中name属性值 必须叫做username 和 password

    四、文件配置(简单的demo)

    1.引入resources-->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>

    <!--页面拦截规则 -->
    <http use-expressions="false">
    <!--拦截所有的路径 只有用户ROLE_USER权限才可以放行-->
    <intercept-url pattern="/**" access="ROLE_USER" />
    <!--login-page :指定登录的页面 default-target-url指定登录成功后 访问的页面 authentication-failure-url:指定登录失败的页面-->
    <form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>

    <csrf disabled="true"/>
    </http>

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

    2.pm.xml核心

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

    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>

    五、将springSecurity引入项目中

    UserDetailServiceImpl :
      由于在springmvc.xml中,引入dubbo时是指向的controller,无法识别service中的@Autowired,且dubbo注解引入必须指向class的上一层
      所以应该使用构造set的方法导入SellerService

    public class UserDetailServiceImpl implements UserDetailsService {
        private SellerService sellerService;
    //构造set方法
    public void setSellerService(SellerService sellerService){
    this.sellerService=sellerService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    //权限的集合
    ArrayList<GrantedAuthority> autList = new ArrayList<>();
    //具有什么样的权限
    autList.add(new SimpleGrantedAuthority("ROLE_SELLER"));
    //1判断用户名是否为空
    if(username==null){
    return null;
    }
    //2根据用户名到数据库查询对应的对象
    if(username!=null){
    //3如果查 不到返回null
    Seller seller = sellerService.findOne(username);
    //4如果用户对象查到了 判断审核是否通过 如果未通过 返回null
    if(seller.getName()!=null&&"1".equals(seller.getStatus())){
    //5返回user对象
    return new User(seller.getName(),seller.getPassword(),autList);
    }
    }
    return null;
    }
    }

    resources-->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"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <http pattern="/*.html" security="none"/>
    <http pattern="/css/**" security="none"/>
    <http pattern="/img/**" security="none"/>
    <http pattern="/js/**" security="none"/>
    <http pattern="/plugins/**" security="none"/>
    <http pattern="/seller/add.do" security="none"/>

    <!-- use-expressions:设置是否启动SpEL表达式,默认值是true。 -->
    <http use-expressions="false">
    <!--
    配置SpringSecurity的拦截路径(拦截规则)
    * pattern:配置拦截规则。 /* 代表的是根路径下的所有资源(不包含子路径) /**代表的是根路径下所有的资源(包含子路径)
    * access:设置角色 角色命名 ROLE_角色名称 如: ROLE_USER
    -->
    <intercept-url pattern="/**" access="ROLE_SELLER"/>

    <!--
    开启表单验证
    username-parameter="username"
    password-parameter="password"
    login-page :登录页面名称 以 / 开始
    default-target-url :登录成功后跳转的页面
    login-processing-url:提交的路径的设置 默认值"/login" 可以修改
    -->
    <form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" always-use-default-target="true"
                      authentication-failure-url="/shoplogin.html"/>

    <!-- 不使用csrf的校验 -->
    <csrf disabled="true"/>

    <!-- 配置框架页面不拦截 -->
    <headers>
    <frame-options policy="SAMEORIGIN"/>
    </headers>

    <!-- 注销的配置 -->
    <logout logout-url="/logout" logout-success-url="/shoplogin.html" />
    </http>

    <!-- 配置认证管理器 -->
    <authentication-manager>
    <!-- 认证的提供者 -->
    <authentication-provider user-service-ref="userDetailService">

    <!--密码加密-->
    <!--<password-encoder ref="passwordEncoder"></password-encoder>-->

    </authentication-provider>
    </authentication-manager>


    <!-- 引用dubbo 服务 -->
    <dubbo:application name="pinyougou-shop-web" />
    <dubbo:registry address="zookeeper://192.168.200.128:2181"/>
    <dubbo:reference id="sellerService" interface="cn.liuhuan.core.service.SellerService" >
    </dubbo:reference>

    <!-- 配置自定义的认证类 -->
    <beans:bean id="userDetailService" class="cn.liuhuan.core.service.UserDetailServiceImpl">
    <beans:property name="sellerService" ref="sellerService"></beans:property>
    </beans:bean>


    <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
    </beans:beans>
  • 相关阅读:
    Direct3D光与材质的颜色值
    Direct中灯光的注意事项
    DirectInput:poll轮询理解
    GetAsyncKeyState函数返回值
    关于PeekMessage中hwnd参数
    VS链接MySql需注意的一些问题(C/C++)
    Windows配置:环境变量是个什么玩意儿?
    项目中ofstream 打开当前文件夹下内容失败原因
    hdoj--2073--无限的路(数学规律)
    hdoj--1205--吃糖果(规律)
  • 原文地址:https://www.cnblogs.com/shiliuhuanya/p/12061606.html
Copyright © 2011-2022 走看看