@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
}
@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”;
}
4.2 @PreAuthorize
①先开启注解功能:
@EnableGlobalMethodSecurity(prePostEnabled = true)
②@PreAuthorize:注解适合进入方法前的权限验证, @PreAuthorize 可以将登录用户的 roles/permissions 参数传到方法中。
@RequestMapping(“/preAuthorize”)
@ResponseBody
//@PreAuthorize(“hasRole(‘ROLE_管理员’)”)
@PreAuthorize(“hasAnyAuthority(‘menu:system’)”)
public String preAuthorize(){
System.out.println(“preAuthorize”);
return “preAuthorize”;
}
4.3@PostAuthorize
①先开启注解功能:
@EnableGlobalMethodSecurity(prePostEnabled = true)
②@PostAuthorize 注解使用并不多,在方法执行后再进行权限验证,适合验证带有返回值
的权限.
@RequestMapping(“/testPostAuthorize”)
@ResponseBody
@PostAuthorize(“hasAnyAuthority(‘menu:system’)”)
public String preAuthorize(){
System.out.println(“test–PostAuthorize”);
return “PostAuthorize”;
}
5. 用户注销
http.logout().logoutUrl(“/logout”).logoutSuccessUrl(“/index”).permitAll
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-Hwylszzq-1715601099998)]
[外链图片转存中…(img-0TljJLJm-1715601099999)]
[外链图片转存中…(img-B5Aw7v5L-1715601099999)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!