项目结构
config
SecurityConfig
package com.wunaiieq.tmp2024121105.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
//security的配置类
@Configuration
public class SecurityConfig {
//密码默认加密,此处配置密码编码器,设置不解析密码
@Bean
public PasswordEncoder passwordEncoder(){
return NoOpPasswordEncoder.getInstance();
}
}
controller
没用过,sercurity会自动联网,使用一个登录页面
PageController
package com.wunaiieq.tmp2024121105.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PageController {
@RequestMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
}
}
entity
Users
package com.wunaiieq.tmp2024121105.entity;
public class Users {
private Integer id;
private String username;
private String password;
private String phone;
public Users() {
}
public Users(Integer id, String username, String password, String phone) {
this.id = id;
this.username = username;
this.password = password;
this.phone = phone;
}
@Override
public String toString() {
return "Users{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
mapper
UsersMapper
package com.wunaiieq.tmp2024121105.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wunaiieq.tmp2024121105.entity.Users;
public interface UsersMapper extends BaseMapper<Users> {
}
service
UserDetailService01
package com.wunaiieq.tmp2024121105.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wunaiieq.tmp2024121105.entity.Users;
import com.wunaiieq.tmp2024121105.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.stereotype.Service;
@Service
public class UserDetailService01 implements UserDetailsService {
@Autowired
private UsersMapper usersMapper;
//自定义认证逻辑
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//1.查询条件
QueryWrapper<Users> wrapper = new QueryWrapper<Users>().eq("username", username);
//2.查询用户
Users users =usersMapper.selectOne(wrapper);
//3.封装为UserDetails对象
UserDetails userDetails = User.withUsername(users.getUsername())
.password(users.getPassword())
.authorities("admin")
.build();
//4.返回封装好的UserDetails对象
return userDetails;
}
}
启动类
package com.wunaiieq.tmp2024121105;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.wunaiieq.tmp2024121105.mapper")
public class Tmp2024121105Application {
public static void main(String[] args) {
SpringApplication.run(Tmp2024121105Application.class, args);
}
}