springboot2.3.1集成springsecurity的最基础demo

springboot2.3.1集成springsecurity的最基础demo

1.简单认识springsecurity

Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入 spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理!
我们首先认识两个类和一个注解:

  • WebSecurityConfigurerAdapter:springsecurity的安全适配器,我们自定义的配置类就是要集成这个适配器
  • AuthenticationManagerBuilder:认证的构造器。我们自定义的配置类中要实现这个方法。
  • @EnableWebSecurity:开启WebSecurity模式

2.使用idea创建一个demo

  • 使用idea创建一个springboot项目,勾选web,springsecurity,thymeleaf。
  • 写一个控制类,跳转页面用
	@GetMapping({"/","/index"})
    public String toLogin(){
        return "/index";
    }


    @GetMapping("/toLoginPage")
    public String toLoginPage(){
        return "/login";
    }

    

    @GetMapping("/toVip/{id}")
    public String toVip(@PathVariable("id") String id) {
        return "/leve" + id + "/" + id;
    }
  • 写一个配置类基础WebSecurityManagerAdpter
package com.guizhou.springsecurity.config;

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.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @Description:
 * @auther: aa
 * @Email: aa@163.com
 * @Date: 2020-6-16 16:26
 * @Copyright: (c) 2019-2022  XXXX公司
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 这个是logout方法报404,关闭csrf跨站请求伪造。如果使用Post请求logout方法,就可以
        // http.csrf().disable();
        // 控制拦截规则.permiall();放行不拦截,
        http.authorizeRequests().antMatchers("/", "/index").permitAll()
        // 必须要某种角色才可以访问的接口地址(目录试了,拦截没有用)
                .antMatchers("/toVip/1").hasRole("vip1")
                .antMatchers("/toVip/2").hasRole("vip2")
                .antMatchers("/toVip/3").hasRole("vip3")
   //用and()分隔。formLogin():没有权限就跳转到登陆界面。loginPage("/toLoginPage"):自己的登陆页面接口。loginProcessingUrl("/login"):登陆成功跳转路径
   // 登陆的默认用户名和密码的name是name=username,name=password。也可以自定义。就是在formLogin()后边继续.usernameParameter("userName").passwordParameter("pwd")
   // loginProcessingUrl("/login"),登陆请求的方法,post提交的时候也是这个地址
   .and().formLogin().loginPage("/toLoginPage").loginProcessingUrl("/login");
		// 开启登出,以及登出成功后请求的路径
        http.logout().logoutSuccessUrl("/");
        // 开启记住我,设置记住我的name(需要设置,才能记住我,默认是14天)
        http.rememberMe().rememberMeParameter("rememberMe");
    }

// 认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    // 做demo的时候是用的内存中的用户,实际中肯定是查询数据库
        auth.inMemoryAuthentication()
        // 密码加密
        .passwordEncoder(new BCryptPasswordEncoder())
        .withUser("user").password(new BCryptPasswordEncoder().encode("user")).roles("vip1")
                .and().withUser("haha").password(new BCryptPasswordEncoder().encode("12")).roles("vip2");
    }
}

注意注意的是springsecurity5,默认是不能使用明文密码,需要加密
这个demo中登陆的方法是springsecurity的,没有自己写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值