Springboot高级(五)安全

在这里插入图片描述

一、

1、引入

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

2、编写配置类

@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    
    

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
    
              //授权
        http.authorizeRequests()
                //所有权限都允许   与/匹配的请求是完全可访问的
                .antMatchers("/").permitAll()
                // 与/level/**匹配的请求需要对用户进行身份验证,并且必须有权限VIP
                .antMatchers("/level/**").hasRole("VIP")
                .and()
                //登录  使用自定义登录页面和失败 URL 启用基于表单的身份验证
                .formLogin().loginPage("/login").failureUrl("/login-error");
        //退出
        http.logout()
                //跳转到指定路径
                .logoutSuccessUrl("/");

        //记住我
        http.rememberMe();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    
    
    	//内存中获取用户
        auth.inMemoryAuthentication()
                //用户                            密码                  权限
                .withUser("zhangsan").password("123456").roles("VIP")
                .and()
                .withUser("lisi").password("123789");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38618691/article/details/118968110