赞
踩
面试成功其实都是必然发生的事情,因为在此之前我做足了充分的准备工作,不单单是纯粹的刷题,更多的还会去刷一些Java核心架构进阶知识点,比如:JVM、高并发、多线程、缓存、Spring相关、分布式、微服务、RPC、网络、设计模式、MQ、Redis、MySQL、设计模式、负载均衡、算法、数据结构、kafka、ZK、集群等。而这些也全被整理浓缩到了一份pdf——《Java核心架构进阶知识点整理》,全部都是精华中的精华,本着共赢的心态,好东西自然也是要分享的
内容颇多,篇幅却有限,这就不在过多的介绍了,大家可根据以上截图自行脑补
FilterSecurityInterceptor: 方法级的权限过滤器, 基本位于过滤链的最底部。
ExceptionTranslationFilter: 异常过滤器,用来处理在认证授权过程中抛出的异常
UsernamePasswordAuthenticationFilter : 对/login 的 POST 请求做拦截,校验表单中用户名,密码。
过滤器的加载过程:DelegatingFilterProxy
方式一:通过配置文件(application.yml)
设置
spring:
security:
user:
name: ly
password: 123456
方式二:通过配置类进行配置
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 创建密码解析器
BCryptPasswordEncoder pe =new BCryptPasswordEncoder();
// 对密码进行加密
String password = pe.encode(“123456”);
auth.inMemoryAuthentication()
.passwordEncoder(pe) //默认没有,需要手动设置BCryptPasswordEncoder
.withUser(“ly”)
.password(password)
.roles(“admin”);
}
}
PasswordEncoder 接口:
把参数按照特定的解析规则进行解析:String encode(CharSequence rawPassword);
验证从存储中获取的编码密码与编码后提交的原始密码是否匹配:boolean matches(CharSequence rawPassword, String encodedPassword); //raw:需要被解析的密码。encode:存储的密码。
判断解析的密码能否再次进行解析且达到更安全的结果:default boolean upgradeEncoding(String encodedPassword) {return false; }
接口实现类BCryptPasswordEncoder 是 Spring Security 官方推荐的密码解析器,是对 bcrypt 强散列方法的具体实现。平时多使用这个解析器。(BCryptPasswordEncoder基于 Hash 算法实现单向加密,可以通过 strength 控制加密强度,默认 10.)
方式三:自定义实现类完成用户登录
UserDetailsService 接口讲解:
而在实际项目中账号和密码都是从数据库中查询出来的。 所以我们要通过自定义逻辑控制认证逻辑。如果需要自定义逻辑时,只需要实现UserDetailsService 接口即可。
返回值 UserDetails ,这个类是系统默认的用户“主体”
User是 UserDetails 实现类,我们只需要使用 User 这个实体类即可:
编写实现类,实现UserDetailsService接口:
/**
@Author: Ly
@Date: 2021-04-16 20:09
*/
@Service(“userDetailsService”) //将MyUserDetailsService注入
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private UsersMapper usersMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
List auths = AuthorityUtils.commaSeparatedStringToAuthorityList(“role”); //配置角色
//用户名,密码可以从数据库查询
return new User(“ly”,new BCryptPasswordEncoder().encode(“123456”),auths);
}
}
修改配置类:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(password());
}
@Bean
PasswordEncoder password(){
return new BCryptPasswordEncoder();
}
}
测试:
创建数据库 security,并创建一个User表:
对应的数据库 Schema 脚本,数据库 Data 脚本如下:
DROP TABLE IF EXISTS users;
CREATE TABLE users(
id BIGINT(20) PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) UNIQUE NOT NULL,
password VARCHAR(100)
);
DELETE FROM users;
INSERT INTO users (id, username, password) VALUES
(1, ‘张三’, ‘123456’),
(2, ‘李四’, ‘123456’),
(3, ‘王五’, ‘123456’);
通过MybatisPlus完成数据库操作: Mybatis-Plus基本使用
添加依赖:
com.baomidou
mybatis-plus-boot-starter
3.3.1.tmp
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
创建实体类Users:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Users {
private Integer id;
private String username;
private String password;
}
新建mapper包,创建UsersMapper接口:
想要使用接口,需要在启动器或配置类上添加注解:@MapperScan("com.ly.mapper")
@Repository
public interface UsersMapper extends BaseMapper {
}
配置文件添加数据库配置 :
spring:
#配置数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/security?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
在实现类中添加数据库相关操作:
@Service(“userDetailsService”)
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private UsersMapper usersMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//调用usersMapper方法,根据用户名查询数据库
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq(“username”,username);
Users users = usersMapper.selectOne(wrapper);
//判断
if(users==null){//数据库没有数据,认证失败
throw new UsernameNotFoundException(“用户名不存在!”);
}
//手动设置了role,也可以通过数据库查询获取
List auths = AuthorityUtils.commaSeparatedStringToAuthorityList(“role”); //配置角色
return new User(users.getUsername(),
new BCryptPasswordEncoder().encode(users.getPassword()),auths);
}
}
测试访问:
自定义登陆页面:
用户名:
用户名:
在配置类实现相关配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
//配置没有权限访问跳转自定义页面
http.exceptionHandling().accessDeniedPage(“/unauth.html”);
http.formLogin() //自定义自己编写的登陆页面
.loginPage(“/login.html”) //登陆页面设置
.loginProcessingUrl(“/user/login”) //登陆访问路径
.defaultSuccessUrl(“/test/hello”).permitAll() //登陆成功后跳转路径
.and().authorizeRequests()
.antMatchers(“/”,“/user/login”).permitAll() //设置那些路径可以直接访问,不需要认证
// .antMatchers(“/test/addUser”).hasAuthority(“addUser”)
// .antMatchers(“/test/findAll”).hasAnyAuthority(“addUser,findAll”)
// .antMatchers(“/test/hello”).hasRole(“admin”)
// .antMatchers(“/test/hello”).hasAnyRole(“admin”)
.anyRequest().authenticated()
.and().csrf().disable(); //关闭csrf的保护
}
权限控制:
1.在配置类设置当前访问地址有那些权限
//当前用户只有具有addUser权限时才能访问该路径
.antMatchers(“/test/add”).hasAuthority(“addUser”)
相关方法: 角色和权限都可以设置多个,以逗号分开
| 方法名称 | 说明 |
| — | — |
| hasAuthority | 如果当前的主体具有指定的权限,则可以访问 |
| hasAnyAuthority | 如果当前的主体有任何提供的角色的话,就可以访问 |
| hasRole | 如果用户具备给定角色就允许访问 |
| hasAnyRole | 用户具备任何一个角色都可以访问 |
2.在UserDetailsService中为User对象设置权限
对于权限可以直接设置,对于角色以ROLE_**
的方式设置
List auths = AuthorityUtils
.commaSeparatedStringToAuthorityList(“addUser,findAll,ROLE_admin,ROLE_user”);
当User对象没有对应权限时会返回403错误:
自定义403页面:
在配置类实现相关配置:http.exceptionHandling().accessDeniedPage("/403.html");
再次测试:
使用注解前需要在启动器或配置类上添加注解:@EnableGlobalMethodSecurity(securedEnabled=true,...)
@SpringBootApplication
@MapperScan(“com.ly.mapper”)
@EnableGlobalMethodSecurity(securedEnabled=true,prePostEnabled = true)
public class SecurityDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SecurityDemoApplication.class, args);
}
}
@Secured:判断是否具有角色:
@RequestMapping(“testSecured”)
@ResponseBody
@Secured({“ROLE_normal”,“ROLE_admin”})
public String testSecured() {
return “testSecured”;
}
登录之后直接访问:http://localhost:10081/test/testSecured
@PreAuthorize:进入方法前进行权限验证, @PreAuthorize 可以将登录用户的 roles/permissions 参数传到方法中。
@RequestMapping(“/preAuthorize”)
@ResponseBody
//@PreAuthorize(“hasRole(‘ROLE_管理员’)”)
@PreAuthorize(“hasAnyAuthority(‘findAll’)”)
public String preAuthorize(){
System.out.println(“preAuthorize”);
return “preAuthorize”;
}
登录之后直接访问:http://localhost:10081/test/preAuthorize
@PostAuthorize:方法执行后再进行权限验证,适合验证带有返回值的权限:
@RequestMapping(“/postAuthorize”)
@ResponseBody
@PostAuthorize(“hasAnyAuthority(‘find’)”)
public String postAuthorize(){
System.out.println(“postAuthorize”);
return “PostAuthorize”;
}
登录之后直接访问:http://localhost:10081/test/postAuthorize
@PostFilter :权限验证之后对数据进行过滤,留下指定的数据,表达式中的 filterObject 引用的是方法返回值 List 中的某一个元素
@RequestMapping(“findAll”)
@PostAuthorize(“hasAnyAuthority(‘findAll’)”)
@PostFilter(“filterObject.username == ‘admin1’”)
@ResponseBody
public List findAllUser(){
ArrayList list = new ArrayList<>();
list.add(new Users(1,“admin1”,“123456”));
list.add(new Users(2,“admin2”,“123456”));
return list;
}
登录之后直接访问:http://localhost:10081/test/findAll
@PreFilter: 进入控制器之前对数据进行过滤
@RequestMapping(“preFilter”)
@PostAuthorize(“hasAnyAuthority(‘findAll’)”)
@PreFilter(value = “filterObject.id%2==0”)
@ResponseBody
public List testPreFilter(@RequestBody List list){
list.forEach(t-> {
System.out.println(t.getId()+“\t”+t.getUsername());
});
return list;
}
先登录,然后使用 postman 进行测试:
测试的 Json 数据:
[
{“id”: “1”,“username”: “admin”,“password”: “666”},
{“id”: “2”,“username”: “admins”,“password”: “888”},
{“id”: “3”,“username”: “admins11”,“password”: “11888”},
{“id”: “4”,“username”: “admins22”,“password”: “22888”}
]
在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。
片描述](https://img-blog.csdnimg.cn/20210419201817479.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0x6eTQxMDk5Mg==,size_16,color_FFFFFF,t_70)
测试的 Json 数据:
[
{“id”: “1”,“username”: “admin”,“password”: “666”},
{“id”: “2”,“username”: “admins”,“password”: “888”},
{“id”: “3”,“username”: “admins11”,“password”: “11888”},
{“id”: “4”,“username”: “admins22”,“password”: “22888”}
]
在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。
[外链图片转存中…(img-p2Io6zYb-1715649388999)]
[外链图片转存中…(img-FuJjijc8-1715649389000)]
[外链图片转存中…(img-ldA0UIXg-1715649389000)]
[外链图片转存中…(img-IKdfeurc-1715649389000)]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。