SpringSecurity---web,那些年我们一起踩过算法与数据结构的坑

@Bean

public PasswordEncoder passwordEncoder(){

return new BCryptPasswordEncoder();

}

}

package com.ty.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.GrantedAuthority;

import org.springframework.security.core.authority.AuthorityUtils;

import org.springframework.security.core.userdetails.User;

import org.springframework.security.core.userdetails.UserDetails;

import org.springframework.security.core.userdetails.UserDetailsService;

import org.springframework.security.core.userdetails.UsernameNotFoundException;

import org.springframework.security.crypto.password.PasswordEncoder;

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class MyUserDetailServiceImp implements UserDetailsService {

@Autowired

PasswordEncoder passwordEncoder;

@Override

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

List list= AuthorityUtils.commaSeparatedStringToAuthorityList(“role”);

return new User(“lisa”, passwordEncoder.encode(“123456”),list);

}

}

3.2 用数据库的用户名和密码

①导入相关的依赖mybatis-plus,connection-mysql,并配置datasource;

②创建数据库和数据表,创建实体类

③创建接口mapper,继承mp 的BaseMapper接口

④完善Service实现类

3.3 自定义登录页面与请求放行

package com.ty.config;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

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.WebSecurityConfigurerAdapter;

import org.springframework.security.core.userdetails.UserDetailsService;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration

public class MySecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired

UserDetailsService userDetailsService;

@Autowired

PasswordEncoder passwordEncoder;

@Override

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http.formLogin()

.loginPage(“/login”)

.loginProcessingUrl(“/index”)

.defaultSuccessUrl(“/hello”)

.failureForwardUrl(“/login”);

http.authorizeRequests()

.antMatchers(“/login”,“/”)

.permitAll()

.anyRequest()

.authenticated();

http.csrf().disable();

}

@Bean

public PasswordEncoder passwordEncoder(){

return new BCryptPasswordEncoder();

}

}

xmlns:th=“http://www.thymeleaf.org”>

Login

Login

登录

3.4 基于角色和权限进行访问控制
3.4.1 hasAuthority 方法

如果当前的主体具有指定的权限,则返回 true,否则返回 false

3.4.2 hasAnyAuthority 方法

如果当前的主体有任何提供的角色(给定的作为一个逗号分隔的字符串列表)的话,返回

true.

在配置类中

.antMatchers(“/find”).hasAuthority(“admin”)

.antMatchers(“/findAll”).hasAnyAuthority(“role,admin”)

在MyUserDetailServiceImp 中

List auths = AuthorityUtils.createAuthorityList(“admin”,“role”);

return new User(“lisa”, passwordEncoder.encode(user.getPassword()),auths);

3.4.3 hasRole 方法

如果用户具备给定角色就允许访问,否则出现 403。

如果当前主体具有指定的角色,则返回 true。

.antMatchers(“/test/hello”).hasRole(“role”)

3.4.4 hasAnyRole 方法

.antMatchers(“/test/find”).hasAnyRole(“ty”)

3.4.5自定义没有权限访问的403页面

http.exceptionHandling().accessDeniedPage(“/403”);

4.认证授权注解
4.1@Secured

判断是否具有角色,另外需要注意的是这里匹配的字符串需要添加前缀“ROLE_“。

①启动类开启注解

@EnableGlobalMethodSecurity(securedEnabled=true)

②在Controller 上面添加注解,配置角色

@RequestMapping(“testSecured”)

@ResponseBody

@Secured({“ROLE_normal”,“ROLE_admin”})

public String helloUser() {

return “hello,user”;

}

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
img

基础面试题

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

主要内容包括:HTML,CSS,JavaScript,浏览器,性能优化等等

686)]
[外链图片转存中…(img-mLyf4P2i-1710837106686)]
[外链图片转存中…(img-SFjNFqdd-1710837106687)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
[外链图片转存中…(img-fFGhShQu-1710837106687)]

基础面试题

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

主要内容包括:HTML,CSS,JavaScript,浏览器,性能优化等等

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值