From 5f7db7f9ac1d960cfb3b1e01b0a97862916d5d51 Mon Sep 17 00:00:00 2001 From: barney <15270405776@163.com> Date: Mon, 23 Sep 2024 13:32:21 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E7=B1=BB=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springinit/config/SecurityConfig.java | 25 ++++++------------- .../springinit/controller/UserController.java | 1 + .../bnblogs/springinit/mapper/UserMapper.java | 2 -- .../service/CustomUserDetailsService.java | 8 ++++++ 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java b/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java index 42cc1dc..962c207 100644 --- a/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java +++ b/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java @@ -1,19 +1,17 @@ package cc.bnblogs.springinit.config; - -import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + + -@EnableWebSecurity -public class SecurityConfig { +@Configuration +public class SecurityConfig extends WebSecurityConfigurerAdapter { - @Bean - public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { + @Override + protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() @@ -26,12 +24,5 @@ public class SecurityConfig { .and() .logout() .permitAll(); - - return http.build(); - } - - @Bean - public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); } } diff --git a/src/main/java/cc/bnblogs/springinit/controller/UserController.java b/src/main/java/cc/bnblogs/springinit/controller/UserController.java index 93e27be..ebb2c8d 100644 --- a/src/main/java/cc/bnblogs/springinit/controller/UserController.java +++ b/src/main/java/cc/bnblogs/springinit/controller/UserController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { + @Autowired private UserService userService; diff --git a/src/main/java/cc/bnblogs/springinit/mapper/UserMapper.java b/src/main/java/cc/bnblogs/springinit/mapper/UserMapper.java index 88b39aa..2676b88 100644 --- a/src/main/java/cc/bnblogs/springinit/mapper/UserMapper.java +++ b/src/main/java/cc/bnblogs/springinit/mapper/UserMapper.java @@ -7,8 +7,6 @@ import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper extends BaseMapper { - @Select("SELECT * FROM users WHERE username = #{username}") MyUser selectByUsername(String username); - } diff --git a/src/main/java/cc/bnblogs/springinit/service/CustomUserDetailsService.java b/src/main/java/cc/bnblogs/springinit/service/CustomUserDetailsService.java index bee909e..d9a4adc 100644 --- a/src/main/java/cc/bnblogs/springinit/service/CustomUserDetailsService.java +++ b/src/main/java/cc/bnblogs/springinit/service/CustomUserDetailsService.java @@ -3,9 +3,12 @@ package cc.bnblogs.springinit.service; import cc.bnblogs.springinit.mapper.UserMapper; import cc.bnblogs.springinit.pojo.MyUser; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; @Service @@ -22,4 +25,9 @@ public class CustomUserDetailsService implements UserDetailsService { } return user; } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } }