diff --git a/pom.xml b/pom.xml index 5c852f2..e275ee6 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.5 + 3.1.2 com.example @@ -52,6 +52,11 @@ spring-boot-starter-test test + + com.baomidou + mybatis-plus-boot-starter + 3.5.3.1 + org.springframework.boot spring-boot-starter-data-redis diff --git a/src/main/java/com/example/config/SecurityConfiguration.java b/src/main/java/com/example/config/SecurityConfiguration.java index 8ac01c2..b842c08 100644 --- a/src/main/java/com/example/config/SecurityConfiguration.java +++ b/src/main/java/com/example/config/SecurityConfiguration.java @@ -1,8 +1,10 @@ package com.example.config; import com.example.entity.RestBean; +import com.example.entity.dto.Account; import com.example.entity.vo.response.AuthorizeV0; import com.example.filter.JwtAuthorizeFilter; +import com.example.service.AccountService; import com.example.utils.JwtUtils; import jakarta.annotation.Resource; import jakarta.servlet.ServletException; @@ -32,6 +34,9 @@ public class SecurityConfiguration { @Resource JwtAuthorizeFilter jwtAuthorizeFilter; + + @Resource + AccountService services; @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http @@ -82,13 +87,13 @@ public class SecurityConfiguration { Authentication authentication) throws IOException, ServletException { response.setContentType("application/json;charset=utf-8"); User user = (User) authentication.getPrincipal(); - - String token = utils.createJwt(user , 1 , "小明"); + Account account = services.findAccountByNameOrEmail(user.getUsername()); + String token = utils.createJwt(user , account.getId(), account.getUsername()); AuthorizeV0 v0 = new AuthorizeV0(); v0.setExpire(utils.expireTime()); - v0.setRole(""); + v0.setRole(account.getRole()); v0.setToken(token); - v0.setUsername("小明"); + v0.setUsername(account.getUsername()); response.getWriter().write(RestBean.success(v0).asJsonString()); } diff --git a/src/main/java/com/example/config/Webconfiguration.java b/src/main/java/com/example/config/Webconfiguration.java new file mode 100644 index 0000000..4c31ed0 --- /dev/null +++ b/src/main/java/com/example/config/Webconfiguration.java @@ -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(); + } +} diff --git a/src/main/java/com/example/entity/dto/Account.java b/src/main/java/com/example/entity/dto/Account.java new file mode 100644 index 0000000..4550843 --- /dev/null +++ b/src/main/java/com/example/entity/dto/Account.java @@ -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; +} diff --git a/src/main/java/com/example/mapper/AccountMapper.java b/src/main/java/com/example/mapper/AccountMapper.java new file mode 100644 index 0000000..9639d4d --- /dev/null +++ b/src/main/java/com/example/mapper/AccountMapper.java @@ -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 { +} diff --git a/src/main/java/com/example/service/AccountService.java b/src/main/java/com/example/service/AccountService.java new file mode 100644 index 0000000..5cae6c1 --- /dev/null +++ b/src/main/java/com/example/service/AccountService.java @@ -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 , UserDetailsService { + public Account findAccountByNameOrEmail(String text); +} diff --git a/src/main/java/com/example/service/impl/AccountServiceImpl.java b/src/main/java/com/example/service/impl/AccountServiceImpl.java new file mode 100644 index 0000000..fb6df79 --- /dev/null +++ b/src/main/java/com/example/service/impl/AccountServiceImpl.java @@ -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 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(); + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 0548fa4..74f9ef0 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -3,4 +3,9 @@ spring: security: jwt: key: abcdefgh - expire: 7 \ No newline at end of file + expire: 7 + datasource: + url: jdbc:mysql://localhost:3306/amadeus + username: root + password: "000000" + driver-class-name: com.mysql.cj.jdbc.Driver diff --git a/src/test/java/com/example/MyProjectBackendApplicationTests.java b/src/test/java/com/example/MyProjectBackendApplicationTests.java index dfe8574..5d724c6 100644 --- a/src/test/java/com/example/MyProjectBackendApplicationTests.java +++ b/src/test/java/com/example/MyProjectBackendApplicationTests.java @@ -2,12 +2,14 @@ package com.example; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @SpringBootTest class MyProjectBackendApplicationTests { @Test void contextLoads() { + System.out.println(new BCryptPasswordEncoder().encode("123456")); } }