From c9a4b9c5a12e200f92e27d758427091303cb25e7 Mon Sep 17 00:00:00 2001 From: barney <15270405776@163.com> Date: Mon, 23 Sep 2024 10:09:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E5=AE=9E=E4=BD=93=E7=B1=BB=E7=BB=A7=E6=89=BFUserDetails?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bnblogs/springinit/mapper/UserMapper.java | 2 ++ .../cc/bnblogs/springinit/pojo/MyUser.java | 36 +++++++++++++++++-- .../service/CustomUserDetailsService.java | 14 +------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/main/java/cc/bnblogs/springinit/mapper/UserMapper.java b/src/main/java/cc/bnblogs/springinit/mapper/UserMapper.java index 2676b88..88b39aa 100644 --- a/src/main/java/cc/bnblogs/springinit/mapper/UserMapper.java +++ b/src/main/java/cc/bnblogs/springinit/mapper/UserMapper.java @@ -7,6 +7,8 @@ import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper extends BaseMapper { + @Select("SELECT * FROM users WHERE username = #{username}") MyUser selectByUsername(String username); + } diff --git a/src/main/java/cc/bnblogs/springinit/pojo/MyUser.java b/src/main/java/cc/bnblogs/springinit/pojo/MyUser.java index 0186ef2..cef65c6 100644 --- a/src/main/java/cc/bnblogs/springinit/pojo/MyUser.java +++ b/src/main/java/cc/bnblogs/springinit/pojo/MyUser.java @@ -1,14 +1,46 @@ package cc.bnblogs.springinit.pojo; + import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import java.util.Collection; +import java.util.Collections; @Data @TableName("users") -public class MyUser { +public class MyUser implements UserDetails { + // 用户表中的字段如下 @TableId private Long id; private String username; private String password; - private Boolean enabled; + + // UserDetails接口需要实现的方法 + @Override + public Collection getAuthorities() { + return Collections.emptyList(); + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } } diff --git a/src/main/java/cc/bnblogs/springinit/service/CustomUserDetailsService.java b/src/main/java/cc/bnblogs/springinit/service/CustomUserDetailsService.java index e2c8701..bee909e 100644 --- a/src/main/java/cc/bnblogs/springinit/service/CustomUserDetailsService.java +++ b/src/main/java/cc/bnblogs/springinit/service/CustomUserDetailsService.java @@ -7,8 +7,6 @@ import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; -import org.springframework.security.core.userdetails.User; -import java.util.Collections; @Service public class CustomUserDetailsService implements UserDetailsService { @@ -22,16 +20,6 @@ public class CustomUserDetailsService implements UserDetailsService { if (user == null) { throw new UsernameNotFoundException("User not found with username: " + username); } - - // 如果不需要处理权限,直接返回用户信息,默认赋予一个角色 - return new User( - user.getUsername(), - user.getPassword(), - user.getEnabled(), - true, - true, - true, - Collections.emptyList() - ); + return user; } }