最后
对于很多Java工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。
整理的这些资料希望对Java开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
再分享一波我的Java面试真题+视频学习详解+技能进阶书籍
重点学习以下几个:
-
WebSecurityConfigurerAdapter:自定义Security策略
-
AuthenticationManagerBuilder:自定义认证策略
-
@EnableWebSecurity:开启WebSecurity模式
Spring Security的两个主要目标是 “认证” 和 “授权”(访问控制)。
“认证”(Authentication)
“授权” (Authorization)
===================================================================
1、新建springboot项目,添加一下模块
2、添加素材
下载SpringSecurity素材:https://www.kuangstudy.com/app/code
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页面。
=====================================================================
新建一个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”);
//
// }
}
======================================================================
最后
学习视频:
大厂面试真题:
习视频:
[外链图片转存中…(img-nts5hpmd-1715509789304)]
大厂面试真题:
[外链图片转存中…(img-GZiBSGqC-1715509789304)]