From 786bbf86221bab0a86130cd2d309b35efba185d8 Mon Sep 17 00:00:00 2001 From: barney <15270405776@163.com> Date: Fri, 20 Sep 2024 18:31:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86=E6=97=A0=E6=95=88=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DemoApplicationConfiguration.java | 52 ------------------- .../springinit/config/SecurityConfig.java | 33 +----------- .../springinit/service/UserService.java | 2 + 3 files changed, 3 insertions(+), 84 deletions(-) delete mode 100644 src/main/java/cc/bnblogs/springinit/DemoApplicationConfiguration.java diff --git a/src/main/java/cc/bnblogs/springinit/DemoApplicationConfiguration.java b/src/main/java/cc/bnblogs/springinit/DemoApplicationConfiguration.java deleted file mode 100644 index b92bd8c..0000000 --- a/src/main/java/cc/bnblogs/springinit/DemoApplicationConfiguration.java +++ /dev/null @@ -1,52 +0,0 @@ -package cc.bnblogs.springinit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.core.authority.SimpleGrantedAuthority; -import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.security.provisioning.InMemoryUserDetailsManager; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -//@Configuration -public class DemoApplicationConfiguration { - - private Logger logger = LoggerFactory.getLogger(DemoApplicationConfiguration.class); - -// @Bean - public UserDetailsService myUserDetailsService() { - - InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager(); - - String[][] usersGroupsAndRoles = { - { "zhangsan", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam" }, - { "lisi", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam" }, - { "wangwu", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam" }, - { "other", "password", "ROLE_ACTIVITI_USER", "GROUP_otherTeam" }, - { "admin", "password", "ROLE_ACTIVITI_ADMIN" }, - }; - - for (String[] user : usersGroupsAndRoles) { - List authoritiesStrings = Arrays.asList(Arrays.copyOfRange(user, 2, user.length)); - logger.info("> Registering new user: " + user[0] + " with the following Authorities[" + authoritiesStrings + "]"); - inMemoryUserDetailsManager.createUser(new User(user[0], passwordEncoder().encode(user[1]), - authoritiesStrings.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList()))); - } - - return inMemoryUserDetailsManager; - - } - -// @Bean - public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } - -} diff --git a/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java b/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java index 3f74c46..42cc1dc 100644 --- a/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java +++ b/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java @@ -1,41 +1,10 @@ package cc.bnblogs.springinit.config; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.context.annotation.Bean; -//import org.springframework.context.annotation.Configuration; -//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -//import org.springframework.security.crypto.password.PasswordEncoder; -// -// -//@Configuration -//public class SecurityConfig extends WebSecurityConfigurerAdapter { -// -// @Override -// protected void configure(AuthenticationManagerBuilder auth) throws Exception { -// BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); -// auth.inMemoryAuthentication(). -// withUser("lucy"). -// password(passwordEncoder.encode("123")).roles("admin"). -// and(). -// withUser("tom") -// .password(passwordEncoder.encode("123")).roles("admin"); -// } -// -// @Bean -// PasswordEncoder password() { -// return new BCryptPasswordEncoder(); -// } -//} -import cc.bnblogs.springinit.service.CustomUserDetailsService; -import org.springframework.beans.factory.annotation.Autowired; + import org.springframework.context.annotation.Bean; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.SecurityFilterChain; diff --git a/src/main/java/cc/bnblogs/springinit/service/UserService.java b/src/main/java/cc/bnblogs/springinit/service/UserService.java index 4890af3..cd85d99 100644 --- a/src/main/java/cc/bnblogs/springinit/service/UserService.java +++ b/src/main/java/cc/bnblogs/springinit/service/UserService.java @@ -3,6 +3,8 @@ 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.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service;