实现数据库用户校验

This commit is contained in:
Amadeus 2024-04-23 23:08:34 +08:00
parent a6d7af1784
commit cafb198c4a
9 changed files with 111 additions and 6 deletions

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version> <version>3.1.2</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>com.example</groupId> <groupId>com.example</groupId>
@ -52,6 +52,11 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>

View File

@ -1,8 +1,10 @@
package com.example.config; package com.example.config;
import com.example.entity.RestBean; import com.example.entity.RestBean;
import com.example.entity.dto.Account;
import com.example.entity.vo.response.AuthorizeV0; import com.example.entity.vo.response.AuthorizeV0;
import com.example.filter.JwtAuthorizeFilter; import com.example.filter.JwtAuthorizeFilter;
import com.example.service.AccountService;
import com.example.utils.JwtUtils; import com.example.utils.JwtUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
@ -32,6 +34,9 @@ public class SecurityConfiguration {
@Resource @Resource
JwtAuthorizeFilter jwtAuthorizeFilter; JwtAuthorizeFilter jwtAuthorizeFilter;
@Resource
AccountService services;
@Bean @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http return http
@ -82,13 +87,13 @@ public class SecurityConfiguration {
Authentication authentication) throws IOException, ServletException { Authentication authentication) throws IOException, ServletException {
response.setContentType("application/json;charset=utf-8"); response.setContentType("application/json;charset=utf-8");
User user = (User) authentication.getPrincipal(); User user = (User) authentication.getPrincipal();
Account account = services.findAccountByNameOrEmail(user.getUsername());
String token = utils.createJwt(user , 1 , "小明"); String token = utils.createJwt(user , account.getId(), account.getUsername());
AuthorizeV0 v0 = new AuthorizeV0(); AuthorizeV0 v0 = new AuthorizeV0();
v0.setExpire(utils.expireTime()); v0.setExpire(utils.expireTime());
v0.setRole(""); v0.setRole(account.getRole());
v0.setToken(token); v0.setToken(token);
v0.setUsername("小明"); v0.setUsername(account.getUsername());
response.getWriter().write(RestBean.success(v0).asJsonString()); response.getWriter().write(RestBean.success(v0).asJsonString());
} }

View File

@ -0,0 +1,14 @@
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
public class Webconfiguration {
//密码解释器
@Bean
BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}

View File

@ -0,0 +1,22 @@
package com.example.entity.dto;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Date;
@Data
@TableName("db_account")
@AllArgsConstructor
public class Account {
@TableId(type = IdType.AUTO)
Integer id;
String username;
String password;
String email;
String role;
Date registerTime;
}

View File

@ -0,0 +1,9 @@
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.dto.Account;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AccountMapper extends BaseMapper<Account> {
}

View File

@ -0,0 +1,10 @@
package com.example.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.dto.Account;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
public interface AccountService extends IService<Account> , UserDetailsService {
public Account findAccountByNameOrEmail(String text);
}

View File

@ -0,0 +1,33 @@
package com.example.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.dto.Account;
import com.example.mapper.AccountMapper;
import com.example.service.AccountService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper , Account> implements AccountService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Account account = this.findAccountByNameOrEmail(username);
if (account == null)
throw new UsernameNotFoundException("用户名或密码错误");
return User
.withUsername(username)
.password(account.getPassword())
.roles(account.getRole())
.build();
}
public Account findAccountByNameOrEmail(String text){
return this.query()
.eq("username",text).or()
.eq("email",text)
.one();
}
}

View File

@ -4,3 +4,8 @@ spring:
jwt: jwt:
key: abcdefgh key: abcdefgh
expire: 7 expire: 7
datasource:
url: jdbc:mysql://localhost:3306/amadeus
username: root
password: "000000"
driver-class-name: com.mysql.cj.jdbc.Driver

View File

@ -2,12 +2,14 @@ package com.example;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@SpringBootTest @SpringBootTest
class MyProjectBackendApplicationTests { class MyProjectBackendApplicationTests {
@Test @Test
void contextLoads() { void contextLoads() {
System.out.println(new BCryptPasswordEncoder().encode("123456"));
} }
} }