zoukankan      html  css  js  c++  java
  • spring中的springSecurity安全框架的环境搭建

    首先在web.xml文件中配置监听器和过滤器

        <!--监听器  加载安全框架的核心配置文件到spring容器中-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/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>

    配置安全框架的核心文件配置

    <?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>
        
        <!-- 配置认证管理器
          1:接收页面传递过来的 用户名 密码 (未加密的)
          2:接收 数据库查询过来的 用户名  密码(加密的)
           在比对之前使用<password-encoder ref="passwordEncoder"/> 来对页面上传递过来的密码进行加密再进行比对
        -->
        <authentication-manager>
            <!-- 认证的提供者 -->
            <authentication-provider user-service-ref="userDetailService">
                <password-encoder ref="passwordEncoder"/>
            </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.itcast.core.service.SellerService" >
        </dubbo:reference>
        <!-- 超时全局设置   10分钟   check=false不检查服务提供方-->
        <dubbo:consumer timeout="600000" check="false"/>
    
    
        <!-- 配置自定义的认证类
         查询Mysql数据库 中 用户名 密码
    
        -->
        <beans:bean id="userDetailService" class="cn.itcast.core.service.UserDetailServiceImpl">
            <beans:property name="sellerService" ref="sellerService"/>
        </beans:bean>
    
    
    
        <!-- 加密的实现类 实例化 -->
        <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
    </beans:beans>

    写出来自定义的认证类

    package com.qingmu.core.service;
    
    import com.qingmu.core.pojo.seller.Seller;
    import org.springframework.security.core.GrantedAuthority;
    import org.springframework.security.core.authority.SimpleGrantedAuthority;
    import org.springframework.security.core.userdetails.User;
    import org.springframework.security.core.userdetails.UserDetails;
    import org.springframework.security.core.userdetails.UserDetailsService;
    import org.springframework.security.core.userdetails.UsernameNotFoundException;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashSet;
    import java.util.List;
    
    /**
     * @Auther:qingmu
     * @Description:脚踏实地,只为出人头地
     * @Date:Created in 17:33 2019/4/12
     */
    public class UserDetailsServiceImpl implements UserDetailsService {
    
        private SellerService sellerService;
    
        public UserDetailsServiceImpl() {
        }
    
        /**
         * 安全框架  自定义认证类
         * 查询数据库中的用户名和密码
         *
         * 作用:根据用户名 查询用户对象,
         * 要求:必须是审核通过的,如果审核不通过或者是未审核,不允许登录
         */
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    
    
            // TODO Auto-generated method stub
            Seller seller = sellerService.findOne(username);
            if(null != seller){
                if("1".equals(seller.getStatus())){
                    //审核通过的用户
                    Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
                    authorities.add(new SimpleGrantedAuthority("ROLE_SELLER"));
    //                参数1.为用户名,参数2.为加密后的密码   返回给认证管理器,由认证管理器来进行判断
                    return new User(username, seller.getPassword(), authorities);
                }
            }
            //无此用户 返回NULL
            return null;
    
        }
    
    
    
        public UserDetailsServiceImpl(SellerService sellerService) {
            this.sellerService = sellerService;
        }
    
        public SellerService getSellerService() {
            return sellerService;
        }
    
        public void setSellerService(SellerService sellerService) {
            this.sellerService = sellerService;
        }
    }

    以上的代码就可以完成一个简单的springSecurity安全框架的自定义

  • 相关阅读:
    maven工程目录结构&常用命令
    slf4j 与 log4j2 实战讲解与日志分割
    跟着大神学zookeeper分布式锁实现-----来自Ruthless
    redis分布式锁Redisson扩展
    Redis分布式锁---完美实现
    redis异常Redis:java.util.NoSuchElementException: Unable to validate object at
    boot中 Quartz注入spring管理类失败
    转载一篇必须超级好的JVM配置实战
    uoj#422. 【集训队作业2018】小Z的礼物(MIn-Max容斥+插头dp)
    uoj#420. 【集训队作业2018】矩形(组合数学)
  • 原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/10698512.html
Copyright © 2011-2022 走看看