SpringSecurity

1.导入pom依赖

<!--security起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

2.创建SecurityConfig类

创建SecurityConfig类 继承 WebSecurityConfigurerAdapter 类并加上 @EnableWebSecurity 注解

package com.lyx.config;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    
}
 

@EnableWebSecurity 的用处

1.加载了WebSecurityConfiguration配置类, 配置安全认证策略。

2: 加载了AuthenticationConfiguration, 配置了认证信息

3.重写WebSecurityConfigurerAdapter

重写WebSecurityConfigurerAdapter 类的

configure(HttpSecurity http)

configure(AuthenticationManagerBuilder auth) 方法

configure()是一个重载方法

@Override
protected void configure(HttpSecurity http) throws Exception {
   //授权规则
}

 @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       //认证规则
}

4.在 configure(HttpSecurity http)方法中定制自己的规则

使用链式编程

authorizeRequests() 认证请求

formLogin() 登录窗口 没有权限会默认到窗口 (自带一个login页面)

antMatchers(String... antPatterns ) 添加一个地址

and() 拼接下一个

permitAll() 准许所有访问

hasRole(String role) 指定哪些角色可以访问

loginPage(String loginPage) 指定登录窗口地址

例子1 . 所有人都可以访问 "/" 下的网页

 @Override
protected void configure(HttpSecurity http) throws Exception {
    
   http.authorizeRequests().antMatchers("/").permitAll()
}

例子2. 如果访问/level1下的地址角色必须是vip1 如果访问/level2下的地址角色必须是vip2

@Override
    protected void configure(HttpSecurity http) throws Exception {
        
       http.authorizeRequests().antMatchers("/level1/**").hasRole("vip1")
       .antMatchers("/level2/**").hasRole("vip2")
           ;
         
    }

例子3.没有权限地址返回/toLogin

@Override
protected void configure(HttpSecurity http) throws Exception {

   http.formLogin().loginPage("/toLogin");
}

5.在 configure(AuthenticationManagerBuilder auth)方法中定制自己的规则

inMemoryAuthentication() 内存认证

jdbcAuthentication() jdbc认证

withUser(String username) 认证的用户名

password(String password) 认证的密码

roles(String... roles) 认证的角色

passwordEncoder(PasswordEncoder passwordEncoder) 设置编码 格式

        示例

/**
     * springboot 2.1.x 可以直接使用
     * 新版本 会报 密码编码:passwordEncoder 错误
     * 在 springboot 5+ 新增了很多加密方式
     * 认证
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().
                passwordEncoder(new BCryptPasswordEncoder())
                .withUser("lyx")
                .password(new BCryptPasswordEncoder().encode("123456"))
                .roles("vip1","vip2","vip3")
                .and()
                .withUser("admin")
                .password(new BCryptPasswordEncoder().encode("123456"))
                .roles("vip1","vip2")
                .and()
                .withUser("user")
                .password(new BCryptPasswordEncoder().encode("123456"))
                .roles("vip1")
        ;
    }

6.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值