@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
登录
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前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
基础面试题
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
主要内容包括:HTML,CSS,JavaScript,浏览器,性能优化等等
686)]
[外链图片转存中…(img-mLyf4P2i-1710837106686)]
[外链图片转存中…(img-SFjNFqdd-1710837106687)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
[外链图片转存中…(img-fFGhShQu-1710837106687)]
基础面试题
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
主要内容包括:HTML,CSS,JavaScript,浏览器,性能优化等等