SpringSecurity框架中用户使用数据库中的用户和密码

SpringSecurity框架的使用和配置文件的书写
这篇博客中我使用框架对登录校验的账户密码是在xml文件中规定死的了
这篇我们来说一下如何结合数据库来校验

  • 其他配置不变,改变xml文件中的配置:
<!--3、配置用户信息(xml  db)-->
<authentication-manager>
    <authentication-provider user-service-ref="loadSellerAccountServce">
            <password-encoder ref="BCryptPasswordEncoder"></password-encoder>
    </authentication-provider>
</authentication-manager>

<beans:bean id="BCryptPasswordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></beans:bean>
<beans:bean id="loadSellerAccountServce" class="com.offcn.shop.componet.LoadSellerAccountService"></beans:bean>
  • 这里我们通过LoadSellerAccountService类来获取用户名和密码
  • LoadSellerAccountService类的书写:
package com.offcn.shop.componet;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.offcn.pojo.TbSeller;
import com.offcn.seller.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;

/**
 * 
 * @create 2021-03-17 21:57
 *
 *
 * 用户提交表单进行登录时需要执行的
 *
 * 在security框架下去数据库中读取商家的账户信息
 *  *  该类需要实现类security的一个接口(注意!!!接口是框架的)
 */

public class LoadSellerAccountService implements UserDetailsService {

//这里使用dubbo远程调用服务,不用管
    @Reference(timeout = 5000)
    SellerService sellerService;

    /**
     *
     * @param sellerId 用户登录传入的用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String sellerId) throws UsernameNotFoundException {
        //1.首先获取数据库中的该用户名的信息
        TbSeller seller = sellerService.updateshow(sellerId);

        //2.1首先判断数据库中是否有该用户
        if (seller == null){
            return  null;
        }

        //2.2如果存在该用户,看其是否被审核通过了
        if (!seller.getStatus().equals("1")){
            return null;
        }

        //2.3 当审核通过时,查看密码是否正确
        String password = seller.getPassword();

        // User 类:  参数3  当前用户的权限集合   Collection<? extends GrantedAuthority> authorities
        // 模拟根据sellerId从数据库中查询它的权限集合
        List<GrantedAuthority> list = this.getAuthrities(sellerId);

        return new User(sellerId,password,list);
        // security框架  会 将用户页面上输入的明文密码和 数据库中password(密文)  做校验


    }

    private List<GrantedAuthority> getAuthrities(String sellerId) {
        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
        // 用户的每一个权限都是用GrantedAuthority类型的对象表示
        SimpleGrantedAuthority authority1 = new SimpleGrantedAuthority("ROLE_SELLER");
        list.add(authority1);
        return list;
    }
}

这样在加载applicationContext-security.xml时,就会调用LoadSellerAccountService,然后我们通过该类获取数据库中的账户密码进行校验了 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值