配置类重构

barney 2 months ago
parent c9a4b9c5a1
commit 5f7db7f9ac
  1. 25
      src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java
  2. 1
      src/main/java/cc/bnblogs/springinit/controller/UserController.java
  3. 2
      src/main/java/cc/bnblogs/springinit/mapper/UserMapper.java
  4. 8
      src/main/java/cc/bnblogs/springinit/service/CustomUserDetailsService.java

@ -1,19 +1,17 @@
package cc.bnblogs.springinit.config; package cc.bnblogs.springinit.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@EnableWebSecurity @Configuration
public class SecurityConfig { public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean @Override
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.csrf().disable() .csrf().disable()
.authorizeRequests() .authorizeRequests()
@ -26,12 +24,5 @@ public class SecurityConfig {
.and() .and()
.logout() .logout()
.permitAll(); .permitAll();
return http.build();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
} }
} }

@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class UserController { public class UserController {
@Autowired @Autowired
private UserService userService; private UserService userService;

@ -7,8 +7,6 @@ import org.apache.ibatis.annotations.Select;
@Mapper @Mapper
public interface UserMapper extends BaseMapper<MyUser> { public interface UserMapper extends BaseMapper<MyUser> {
@Select("SELECT * FROM users WHERE username = #{username}") @Select("SELECT * FROM users WHERE username = #{username}")
MyUser selectByUsername(String username); MyUser selectByUsername(String username);
} }

@ -3,9 +3,12 @@ package cc.bnblogs.springinit.service;
import cc.bnblogs.springinit.mapper.UserMapper; import cc.bnblogs.springinit.mapper.UserMapper;
import cc.bnblogs.springinit.pojo.MyUser; import cc.bnblogs.springinit.pojo.MyUser;
import org.springframework.beans.factory.annotation.Autowired; 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.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException; 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; import org.springframework.stereotype.Service;
@Service @Service
@ -22,4 +25,9 @@ public class CustomUserDetailsService implements UserDetailsService {
} }
return user; return user;
} }
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
} }

Loading…
Cancel
Save