泡泡SpringSecurity2.5【认证状态】

 


 

 

 

 

 

 

 

 


 

复制代码
package com.haifei.service.impl;

import com.haifei.mapper.UserMapper;
import com.haifei.pojo.User;
import com.haifei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 该方法来自UserService接口的父接口UserDetailsService,必须重写(覆盖)
     */
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //根据账号查找用户信息
        User user = userMapper.findUserByUsername(s);
        if (user != null){
            //设置登录账号的角色
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            authorities.add(new SimpleGrantedAuthority("ROLE_USER"));

            /*UserDetails userDetails = new org.springframework.security.core.userdetails.User(
                    user.getUsername(),
//                    "{noop}"+user.getPassword(), //不加密
                    user.getPassword(), //加密
                    authorities
            );*/

            UserDetails userDetails = new org.springframework.security.core.userdetails.User(
                    user.getUsername(),
                    user.getPassword(),
                    true, //是否可用
                    true, //账号是否过期
                    true, //凭证是否过期
                    true, //是否被锁定
                    authorities
            );
            return userDetails;
        }
        return null;
    }


    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        System.out.println(encoder.encode("123"));
        System.out.println(encoder.encode("123"));
        System.out.println(encoder.encode("123"));
        /*
        同一个明文的密文是不同的
        $2a$10$kckLzrC/LICmbFYurp6bZ.w2DYUmNou6k.ANXOHXGw6MNlyBlatc.
        $2a$10$RiZX6Y/W5G.e39sj392gOOjZpv.BQCiPKiPMvfP8B8ZrKFv5/PVPO
        $2a$10$9bOOecBUR4VuTWN0H/alZuEjrG/l2iib.NUesTIGLL4SE470RItEy
         */
    }

}
View Code
复制代码

 

 

 

 

 

 

 

 

 

 

 登录失败

 

 

 

 

 

 

 

 登录成功

 


 

如果要做动态的,可以在用户的表结构中添加相关的字段来维护这种关系

boolean enabled
boolean accountNonExpired
boolean credentialsNonExpired
boolean accountNonLocked

 

posted @   yub4by  阅读(51)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示