配置类重构

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;
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();
}
}

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

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

@ -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();
}
}

Loading…
Cancel
Save