玩转SpringBoot安全管理:SpringSecurity之自定义用户授权管理

时间:2025-04-02 12:58:23
package com.security.config; import com.security.service.UserDetailsServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer; import org.springframework.security.config.annotation.authentication.configurers.provisioning.JdbcUserDetailsManagerConfigurer; 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 javax.sql.DataSource; /** * @program: spring security * @description: security配置类 * @author: xmonster_大魔王 * @create: 2022-08-02 11:41 **/ @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Qualifier("dataSource") @Autowired private DataSource dataSource; @Autowired private UserDetailsServiceImpl userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // 设置密码编码器 BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/detail/common/**").hasRole("common") .antMatchers("/detail/vip/**").hasRole("vip") .anyRequest().authenticated() .and().formLogin(); } }