首页>代码>SpringBoot整合Security极简入门实例>/springboot-security-demo/src/main/java/com/simon/springbootsecuritydemo/config/SecurityConfig.java
package com.simon.springbootsecuritydemo.config;

import org.springframework.beans.factory.annotation.Autowired;
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.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author Simon
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /** 从数据库设置用户权限测试 */
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/druid/**").permitAll()
                .antMatchers("/level1/**").hasAuthority("1")
                .antMatchers("/level2/**").hasAuthority("2")
                .antMatchers("/level3/**").hasAuthority("3");
        http.formLogin().loginPage("/toLogin")
                .usernameParameter("username").passwordParameter("password")
                .loginProcessingUrl("/login");

        http.logout().logoutSuccessUrl("/");
        http.rememberMe().rememberMeParameter("remember");
        http.csrf().disable();
    }

    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }
}
最近下载更多
励志12345  LV2 2023年8月29日
Rommel  LV27 2022年11月27日
最代码官方  LV167 2022年11月26日
最近浏览更多
Hachi6  LV13 2023年9月8日
2131234536546  LV7 2023年9月8日
励志12345  LV2 2023年8月29日
另类清晨  LV8 2023年8月19日
www2222  LV2 2023年8月9日
lipanlong  LV10 2023年7月6日
Weishenghui  LV7 2023年6月10日
微信网友_6508798513811456  LV5 2023年6月8日
szf123  LV12 2023年5月30日
做你的景天  LV7 2023年4月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友