1. 配置数据库
-
相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3.1</version>
</dependency>
-
在application.properties中添加数据库配置
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/kob?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-
在ideal中配置连接数据库
{:height=”80%” width=”80%”}
2. Mybatis代码层部署
-
pojo层示例
package com.kob.backend.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
private String username;
private String password;
private String photo;
}
-
Mapper层示例
package com.kob.backend.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import com.kob.backend.pojo.User;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
-
Controller层示例
标准动作
package com.bol.lyh_backend.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bol.lyh_backend.Mapper.UserMapper;
import com.bol.lyh_backend.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/user/")
public class UserController {
@Autowired
UserMapper userMapper;
@RequestMapping("allinfo/")
public List<User> getAll(){
return userMapper.selectList(null);
}
@RequestMapping("getone/{userid}/")
public User getuser(@PathVariable int userid){
/*
方法一:
return userMapper.selectById(userid);
*/
//方法二:
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id",userid);
return userMapper.selectOne(queryWrapper);
}
@RequestMapping("getRange/{userge}/{userle}/")
public List<User> selectUser(
@PathVariable int userge,
@PathVariable int userle
){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.ge("id",userge).le("id",userle);//返回id在[l,r]上的所有用户
return userMapper.selectList(queryWrapper);
}
@RequestMapping("add/{userid}/{username}/{password}/")
public String addUser(
@PathVariable int userid,
@PathVariable String username,
@PathVariable String password
){
User user = new User(userid,username,password);
userMapper.insert(user);
return "Add Success";
}
@RequestMapping("delete/{userid}")
public String deleteUser(@PathVariable int userid){
userMapper.deleteById(userid);
return "Delete Success";
}
}
3. 配置用户认证操作(实现安全机制)
-
相关依赖
spring boot自带的登录页面
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>3.0.6</version>
</dependency>
-
修改Spring Security,对接数据库
修改流程见讲义
关于UserDetailsServiceImpl类
{:height=”80%” width=”80%”}
工具类UserDetailsServiceImpl代码
package com.bol.lyh_backend.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bol.lyh_backend.Mapper.UserMapper;
import com.bol.lyh_backend.pojo.User;
import com.bol.lyh_backend.service.impl.utils.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
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 UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username",username);
User user = userMapper.selectOne(queryWrapper);
if(user == null) {
throw new RuntimeException("用户不存在");
}
System.out.println(user);
return new UserDetailsImpl(user);
}
}
还需再实现一个工具类UserDetailsImpl
package com.bol.lyh_backend.service.impl.utils;
import com.bol.lyh_backend.pojo.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDetailsImpl implements UserDetails {
private User 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;
}
}
报There is no PasswordEncoder mapped for the id “null”错很正常
{:height=”80%” width=”80%”}
-
数据库明文加密
配置类SecurityConfig,一般放在config目录下
package com.bol.lyh_backend.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
使用配置类SecurityConfig进行明文加密,示例代码如下
@RequestMapping("add/{userid}/{username}/{password}/")
public String addUser(
@PathVariable int userid,
@PathVariable String username,
@PathVariable String password
){
//Update encodePassword
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode(password);
User user = new User(userid,username,encodedPassword);
userMapper.insert(user);
return "Add Success";
}
4. 配置Jwt验证
-
相关依赖
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
-
实现工具类
JwtUtil,用来创建、解析jwt token
JwtAuthenticationTokenFilter,用来验证jwt token,如果验证成功,则将User信息注入上下文中
升级之前的SecurityConfig类 新SecurityConfig,放行登录、注册等接口
在升级后的配置类SecurityConfig放行链接
{:height=”80%” width=”80%”}
5. 编写用户的三个API接口
note: Service层一般存放接口,接口的实现一般放impl里
-
实现LoginServiceImpl类,用于给登录成功的用户生成jwt token
Alt+Ins键快速实现方法
标准动作
package com.bol.lyh_backend.service.impl.user.account;
import com.bol.lyh_backend.pojo.User;
import com.bol.lyh_backend.service.impl.utils.UserDetailsImpl;
import com.bol.lyh_backend.service.user.account.LoginService;
import com.bol.lyh_backend.utils.JwtUtil.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service //介个不能忘
public class LoginServiceImpl implements LoginService {
@Autowired
private AuthenticationManager authenticationManager;//自带
@Override
public Map<String, String> getToken(String username, String password) {
//自带,把username和password封装成一个类,本质是变成加密之后的字符串
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(username,password);
//自带,验证封装后的类能否正常登录
Authentication authenticate = authenticationManager.authenticate(authenticationToken);//登陆失败自动报异常,不用管
//能运行到这说明登录成功了
//以下属于模板类代码,cv即可
//取出登录成功后的用户
UserDetailsImpl loginUser = (UserDetailsImpl) authenticate.getPrincipal();
User user = loginUser.getUser();
//使用之前导入的工具类的方法,创建jwt
String jwt = JwtUtil.createJWT(user.getId().toString());
Map<String,String> map = new HashMap<>();
map.put("error_message","success");
map.put("token",jwt);
return map;
}
}
Controller层调用
package com.bol.lyh_backend.controller.user.account;
import com.bol.lyh_backend.service.user.account.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class LoginController {
@Autowired
private LoginService loginService;
@PostMapping("/user/account/token/")//注意!此处的链接要放行!!
public Map<String,String> getToken(@RequestParam Map<String,String> map){
//@RequestParam代表从前端获取参数
String username=map.get("username");
String password=map.get("password");
return loginService.getToken(username,password);
}
}
前端调试代码示例如下
setup(){
$.ajax({
url:"http://127.0.0.1:3000/user/account/token/",
type:"post",
data:{
username:"b33",
password:"33"
},
success(resp){
console.log(resp);
},
error(){
console.log("失败啦!");
}
});
}
}
-
实现InfoServiceImpl类,用于获取到经过身份验证的用户信息
标准动作
package com.bol.lyh_backend.service.impl.user.account;
import com.bol.lyh_backend.pojo.User;
import com.bol.lyh_backend.service.impl.utils.UserDetailsImpl;
import com.bol.lyh_backend.service.user.account.InfoService;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class InfoServiceImpl implements InfoService {
@Override
public Map<String, String> getinfo() {
//模板代码,最终的user即是获取到经过身份验证的用户信息对象
UsernamePasswordAuthenticationToken authentication =
(UsernamePasswordAuthenticationToken) SecurityContextHolder
.getContext()
.getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();
User user = loginUser.getUser();
Map<String,String> map = new HashMap<>();
map.put("error_message","success");
map.put("id",user.getId().toString());
map.put("username",user.getUsername());
map.put("password",user.getPassword());
map.put("photo",user.getPhoto());
return map;
}
}
Controller层调用
package com.bol.lyh_backend.controller.user.account;
import com.bol.lyh_backend.service.user.account.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class InfoController {
@Autowired
private InfoService infoService;
@GetMapping("/user/account/info/")//GetMapping好像不用放行
public Map<String,String> getinfo(){
return infoService.getinfo();
}
}
前端调试代码示例如下
$.ajax({
url:"http://127.0.0.1:3000/user/account/info/",
type:"get",
headers:{
Authorization: "Bearer "+"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1NTA4ZGJlZmJjNjI0ZWNjOTJmYzRkOWI4MjdjZTE5OSIsInN1YiI6IjMiLCJpc3MiOiJzZyIsImlhdCI6MTY4MzQ1NzU0MywiZXhwIjoxNjg0NjY3MTQzfQ.tyACjKX9vdnWJRNqifqL77mAGs5M2ljD9cMw-Frx18w"
},
success(resp){
console.log(resp);
},
error(){
console.log("第二个失败啦!");
}
});
这个Authorization的token是用LoginController类的getToken方法获得的
Bearer后面记得加空格
-
实现RegisterServiceImpl类,用于用户注册
package com.bol.lyh_backend.service.impl.user.account;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bol.lyh_backend.Mapper.UserMapper;
import com.bol.lyh_backend.pojo.User;
import com.bol.lyh_backend.service.user.account.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
@Service
public class RegisterServiceImpl implements RegisterService {
@Autowired UserMapper userMapper;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public Map<String, String> register(String username, String password, String confirmedPassword) {
Map<String,String> map = new HashMap<>();
if(username == null){
map.put("error_message","用户名不能为空");
return map;
}
if(password==null||confirmedPassword == null){
map.put("error_message","密码不能为空");
return map;
}
username = username.trim();//删掉首尾空白字符
if(username.length()==0){
map.put("error_message","用户名不能为空");
return map;
}
if(username.length()>60){
map.put("error_message","用户名长度不能大于60");
return map;
}
if(password.length()>60||confirmedPassword.length()>60){
map.put("error_message","密码长度不能大于60");
return map;
}
if(!password.equals(confirmedPassword)){
map.put("error_message","两次密码不一致");
return map;
}
//查询的标准动作
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username",username);
List<User> users = userMapper.selectList(queryWrapper);
if(!users.isEmpty()){
map.put("error_message","用户名已存在");
return map;
}
//特判完成,开始写入数据库
String encodedPassword = passwordEncoder.encode(password);
String photo = "https://cdn.acwing.com/media/user/profile/photo/93679_lg_c871e15988.jpg";
User user = new User(null,username,encodedPassword,photo);//id自增所以不用管
userMapper.insert(user);
map.put("error_message","success");
return map;
}
}
Controller层调用
package com.bol.lyh_backend.controller.user.account;
import com.bol.lyh_backend.service.user.account.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class RegisterController {
@Autowired
private RegisterService registerService;
@PostMapping("/user/account/register/")//记得放行
public Map<String,String> register(@RequestParam Map<String,String> map){
String username = map.get("username");
String password = map.get("password");
String confirmedPassword = map.get("confirmedPassword");
return registerService.register(username,password,confirmedPassword);
}
}
前端调试代码同LoginServiceImpl11