單獨給實現安全框架這一組拿出來,供大家使用參考
依賴
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
重寫UserDetailsService(效驗)
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Override
public UserDetails loadUserByUsername(String username) throws
UsernameNotFoundException {
//根據使用者名稱查詢使用者資訊
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getUserName,username);
User user = userMapper.selectOne(wrapper);
//如果查詢不到資料就透過丟擲異常來給出提示
if(Objects.isNull(user)){
throw new RuntimeException("使用者名稱或密碼錯誤");
}
// //TODO 根據使用者查詢許可權資訊 新增到LoginUser中
//
// User user=new User();
// user.setUserName(username);
// //封裝成UserDetails物件返回
return new LoginUser(user);
}
}
返回物件
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginUser implements UserDetails {
private User user;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getUserName();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
model
package com.example.openai.model;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* 使用者表(User)實體類
*
* @author 三更
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_user")
public class User implements Serializable {
private static final long serialVersionUID = -40356785423868312L;
/**
* 主鍵
*/
@TableId
private Long id;
/**
* 使用者名稱
*/
private String userName;
/**
* 暱稱
*/
private String nickName;
/**
* 密碼
*/
private String password;
/**
* 賬號狀態(0正常 1停用)
*/
private String status;
/**
* 郵箱
*/
private String email;
/**
* 手機號
*/
private String phonenumber;
/**
* 使用者性別(0男,1女,2未知)
*/
private String sex;
/**
* 頭像
*/
private String avatar;
/**
* 使用者型別(0管理員,1普通使用者)
*/
private String userType;
/**
* 建立人的使用者id
*/
private Long createBy;
/**
* 建立時間
*/
private Date createTime;
/**
* 更新人
*/
private Long updateBy;
/**
* 更新時間
*/
private Date updateTime;
/**
* 刪除標誌(0代表未刪除,1代表已刪除)
*/
private Integer delFlag;
}
mapper
package com.example.openai.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.openai.model.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/java20?characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver