spring Security的自定义用户认证过程详解

网友投稿 322 2023-07-19

spring Security的自定义用户认证过程详解

spring Security的自定义用户认证过程详解

首先我需要在xml文件中声明.我要进行自定义用户的认证类,也就是我要自己从数据库中进行查询

WJMrdUICWy

配置完自定义的文件以后,在需要自定义认证类的模块中实现

UserDetailsService

package com.qingmu2.core.service;

import com.alibaba.dubbo.config.annotation.Reference;

import com.qingmu2.core.pojo.seller.Seller;

import org.springframework.beans.factory.annotation.Autowired;

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.HashMap;

import java.util.HashSet;

import java.util.List;

/**

* 自定义的认证类

* @Auther:qingmu

* @Description:脚踏实地,只为出人头地

* @Date:Created in 8:33 2019/5/31

*/

public class UserDetailServiceImpl implements UserDetailsService {

private SellerService sellerService;

public UserDetailServiceImpl() {

}

@Override

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

Seller seller = sellerService.findOne(username);

if(null!=seller){

//判断一次商家是否被审核通过.

if("1".equals(seller.getStatus())){

//创建一个集合,用来存储权限

HashSet authorities = new HashSet<>();

authorities.add(new SimpleGrantedAuthority("ROLE_SELLER"));

//将这个用户的信息返回给认证类

return new User(username,seller.getPassword(),authorities);

}

}

//没有这个用户,则返回null

return null;

}

public UserDetailServiceImpl(SellerService sellerService) {

this.sellerService = sellerService;

}

public SellerService getSellerService() {

return sellerService;

}

public void setSellerService(SellerService sellerService) {

this.sellerService = sellerService;

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JFinal使用ajaxfileupload实现图片上传及预览
下一篇:SpringBoot如何指定某些类优先启动
相关文章

 发表评论

暂时没有评论,来抢沙发吧~