zoukankan      html  css  js  c++  java
  • springSecurity自定义认证配置

    上一篇讲了springSecurity的简单入门的小demo,认证用户是在xml中写死的。今天来说一下自定义认证,读取数据库来实现认证。当然,也是非常简单的,因为仅仅是读取数据库,权限是写死的,因为相对简单,没几个角色,就直接写死了。

    还有就是加密,使用的是框架自带的   BCryptPasswordEncoder   加密方法。存在数据库的用户密码也是通过这个类加密,然后登陆的时候也是通过这个类验证,需要在xml中配置下就ok。

    简单说一下这个加密类。比md5更加的高级。

    加密分为  :

    可逆(秘钥)

    不可逆(哈希算法)(BCryptPasswordEncoder 和md5 都属于 )

    理论上md5是不可逆的,但是其实上是可以破解的,如果不想被破解,可以采用加盐的方法,BCryptPasswordEncoder就是自己生成随机盐,即使密码一样得到的加密后的密码也不一样,这大大增加了破解的难度。

    大体步骤:

    自己写一个类实现   UserDetailsService  这个接口  ,实现一个方法,最主要是返回一个User对象,这个对象是框架提供的。具体看代码。

    然后将这个自定义的类在xml中配置一下,就是说原来写死的用户信息删除掉,采用这个类来验证。 就ok  非常简单

    UserDetailsServiceImpl.java    // 认证类

    package com.pinyougou.service;
    
    import com.pinyougou.pojo.TbSeller;
    import com.pinyougou.sellergoods.service.SellerService;
    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.List;
    /**
     * 认证类
     * @author Administrator
     *
     */
    public class UserDetailsServiceImpl implements UserDetailsService {
    
    
        private SellerService sellerService;
        
        public void setSellerService(SellerService sellerService) {
            this.sellerService = sellerService;
        }
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            System.out.println("经过了UserDetailsServiceImpl");
            //构建角色列表
            List<GrantedAuthority> grantAuths=new ArrayList();
            grantAuths.add(new SimpleGrantedAuthority("ROLE_SELLER"));
            
            //得到商家对象
            TbSeller seller = sellerService.findOne(username);
            if(seller!=null){
                if(seller.getStatus().equals("1")){
                    return new User(username,seller.getPassword(),grantAuths);
                }else{
                    return null;
                }            
            }else{
                return null;
            }
        }
    
    }

    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:dubbo="http://code.alibabatech.com/schema/dubbo"
        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://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>
        <http pattern="/css/**" security="none"></http>
        <http pattern="/img/**" security="none"></http>
        <http pattern="/js/**" security="none"></http>
        <http pattern="/plugins/**" security="none"></http>
        <http pattern="/seller/add.do" security="none"></http>
    
        <!-- 页面的拦截规则    use-expressions:是否启动SPEL表达式 默认是true -->
        <http use-expressions="false">
            <!-- 当前用户必须有ROLE_USER的角色 才可以访问根目录及所属子目录的资源 -->
            <intercept-url pattern="/**" access="ROLE_SELLER"/>
            <!-- 开启表单登陆功能 -->
            <form-login  login-page="/shoplogin.html" default-target-url="/admin/index.html" authentication-failure-url="/shoplogin.html" always-use-default-target="true"/>
            <csrf disabled="true"/>
            <headers>
                <frame-options policy="SAMEORIGIN"/>
            </headers>
            <logout/>
        </http>
        
        <!-- 认证管理器 -->
        <authentication-manager>
            <authentication-provider user-service-ref="userDetailService">
                <password-encoder ref="bcryptEncoder"></password-encoder>
            </authentication-provider>    
        </authentication-manager>
            
        <!-- 自定义认证类 -->
        <beans:bean id="userDetailService" class="com.pinyougou.service.UserDetailsServiceImpl">
            <beans:property name="sellerService" ref="sellerService"></beans:property>
        </beans:bean>
        
        <!-- 引用dubbo 服务 因为认证类需要用到这个服务,但是这个服务是在dubbo中的,远程的,所以需要采用这种方法来引入,不能
         在向以前那样直接spring注解的方法引入了,切记。-->
        <dubbo:application name="pinyougou-shop-web" />
        <dubbo:registry address="zookeeper://47.98.157.114:2181"/>
        <dubbo:reference id="sellerService" interface="com.pinyougou.sellergoods.service.SellerService"></dubbo:reference>
    
        <!--加密类-->
        <beans:bean id="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></beans:bean>
    </beans:beans>








  • 相关阅读:
    有序数组中找中位数
    卡特兰数 catalan number
    海量数据等概率选取问题
    求字符串中最长无重复字符的子串
    Linux的进程通信(IPC)
    Linux多线程编程
    后缀数组处理字符串的利器
    网络编程socket基本API详解
    Windows线程的创建与终止
    《算法导论》读书笔记之第10章 基本数据结构之二叉树
  • 原文地址:https://www.cnblogs.com/coder-lzh/p/9125028.html
Copyright © 2011-2022 走看看