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

总结

至此,文章终于到了尾声。总结一下,我们谈论了简历制作过程中需要注意的以下三个部分,并分别给出了一些建议:

  1. 技术能力:先写岗位所需能力,再写加分能力,不要写无关能力;
  2. 项目经历:只写明星项目,描述遵循 STAR 法则;
  3. 简历印象:简历遵循三大原则:清晰,简短,必要,要有的放矢,不要海投;

以及最后为大家准备的福利时间:简历模板+Java面试题+热门技术系列教程视频

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

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

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”);

//

// }

}

控制显示和隐藏

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

领取资料

开启注销功能

//开启注销功能,跳转到首页

http.logout().logoutSuccessUrl(“/”);

image

导入thymeleaf和springsecurity5整合

org.thymeleaf.extras

thymeleaf-extras-springsecurity5

3.0.4.RELEASE

领取资料

修改前端

登录

最后

手绘了下图所示的kafka知识大纲流程图(xmind文件不能上传,导出图片展现),但都可提供源文件给每位爱学习的朋友

image.png

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

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

手绘了下图所示的kafka知识大纲流程图(xmind文件不能上传,导出图片展现),但都可提供源文件给每位爱学习的朋友

[外链图片转存中…(img-QXC7j3gO-1715509873172)]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值