史上最全SpringBoot教程,从零开始带你深入♂学习(十四)——集成SpringSecurity(1)

最后

对于很多Java工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。

整理的这些资料希望对Java开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

再分享一波我的Java面试真题+视频学习详解+技能进阶书籍

美团二面惜败,我的凉经复盘(附学习笔记+面试整理+进阶书籍)

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

领取资料

重点学习以下几个:
  • WebSecurityConfigurerAdapter:自定义Security策略

  • AuthenticationManagerBuilder:自定义认证策略

  • @EnableWebSecurity:开启WebSecurity模式

Spring Security的两个主要目标是 “认证” 和 “授权”(访问控制)。

“认证”(Authentication)

“授权” (Authorization)

环境搭建

===================================================================

领取资料

1、新建springboot项目,添加一下模块

image

2、添加素材

下载SpringSecurity素材:https://www.kuangstudy.com/app/code

image

领取资料

3、编写controller层

package com.study.controller;

//加群1025684353一起吹水聊天

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class RouterController {

@RequestMapping({“/”,“/index”})

public String index(){

return “index”;

}

@RequestMapping(“/toLogin”)

public String toLogin(){

return “views/login”;

}

//添加参数,实现多页面跳转

@RequestMapping(“/level1/{id}”)

public String level1(@PathVariable(“id”) int id){

return “views/level1/”+id;

}

@RequestMapping(“/level2/{id}”)

public String level2(@PathVariable(“id”) int id){

return “views/level2/”+id;

}

//加群1025684353一起吹水聊天

@RequestMapping(“/level3/{id}”)

public String level3(@PathVariable(“id”) int id){

return “views/level3/”+id;

}

}

运行测试:输入localhost:8080

领取资料

发现,页面自动跳转到login页面,那么只要引入SpringSecurity模块,输入任何页面都会自动跳转到SpringSecurity默认提供的login页面。

image

角色认证授权

=====================================================================

新建一个SecurityConfig配置类,继承WebSecurityConfigurerAdapter,再添加@EnableWebSecurity注解

package com.study.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;

//拦截器

@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {

//加群1025684353一起吹水聊天

//权限拦截

@Override

protected void configure(HttpSecurity http) throws Exception {

//首页所有人可以访问,功能页只有对应权限的人才可以访问

// 认证请求

http.authorizeRequests()

//请求地址(“/”):首页 所有人都可以访问

.antMatchers(“/”).permitAll()

//求情地址(“/level1/**”):level1目录下所有页面 只有vip1权限的角色可以访问

.antMatchers(“/level1/**”).hasAnyRole(“vip1”)

.antMatchers(“/level2/**”).hasAnyRole(“vip2”)

.antMatchers(“/level3/**”).hasAnyRole(“vip3”)

//除此之外,所有请求都必须要认证才能访问

// 所有请求

.anyRequest().authenticated();

//没有权限默认会到登录页面,需要开启登录的页面

http.formLogin();

}

//认证

@Override

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

//auth.jdbcAuthentication():数据库

//内存里认证

//这些数据正常应该从数据库中读取 密码加密方式,防止反编译

auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())

//设置用户密码和赋予权限 密码编码

.withUser(“study”).password(new BCryptPasswordEncoder().encode(“123456”)).roles(“vip2”,“vip3”)

.and() //拼接多个用户

.withUser(“root”).password(new BCryptPasswordEncoder().encode(“123456”)).roles(“vip1”,“vip2”,“vip3”)

.and()//加群1025684353一起吹水聊天

.withUser(“guest”).password(new BCryptPasswordEncoder().encode(“123456”)).roles(“vip3”);

}

//----------------------------------------------------------------------

// 链接数据库

// private DataSource dataSource;

//

// @Override

// protected void configure(AuthenticationManagerBuilder auth) throws Exception {

// auth.jdbcAuthentication()

// .dataSource(dataSource)

// .withDefaultSchema()

// .withUser(“user”).password(“password”).roles(“USER”)

// .and()

// .withUser(“admin”).password(“password”).roles(“USER”,“ADMIN”);

//

// }

}

控制显示和隐藏

======================================================================

领取资料

最后

学习视频:

大厂面试真题:

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

习视频:

[外链图片转存中…(img-nts5hpmd-1715509789304)]

大厂面试真题:

[外链图片转存中…(img-GZiBSGqC-1715509789304)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 27
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值