如何使用SpringSecurity保护程序安全
作者:_元夕 发布时间:2022-09-08 19:57:50
首先,引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
引入此依赖之后,你的web程序将拥有以下功能:
所有请求路径都需要认证
不需要特定的角色和权限
没有登录页面,使用HTTP基本身份认证
只有一个用户,名称为user
配置SpringSecurity
springsecurity配置项,最好保存在一个单独的配置类中:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}
配置用户认证方式
首先,要解决的就是用户注册,保存用户的信息。springsecurity提供四种存储用户的方式:
基于内存(生产肯定不使用)
基于JDBC
基于LDAP
用户自定义(最常用)
使用其中任意一种方式,需要覆盖configure(AuthenticationManagerBuilder auth)方法:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}
}
1.基于内存
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("zhangsan").password("123").authorities("ROLE_USER")
.and()
.withUser("lisi").password("456").authorities("ROLE_USER");
}
2.基于JDBC
@Autowired
DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource);
}
基于JDBC的方式,你必须有一些特定表表,而且字段满足其查询规则:
public static final String DEF_USERS_BY_USERNAME_QUERY =
"select username,password,enabled " +
"from users " +
"where username = ?";
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY =
"select username,authority " +
"from authorities " +
"where username = ?";
public static final String DEF_GROUP_AUTHORITIES_BY_USERNAME_QUERY =
"select g.id, g.group_name, ga.authority " +
"from groups g, group_members gm, group_authorities ga " +
"where gm.username = ? " +
"and g.id = ga.group_id " +
"and g.id = gm.group_id";
当然,你可以对这些语句进行一下修改:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username, password, enabled from Users " +
"where username=?")
.authoritiesByUsernameQuery("select username, authority from UserAuthorities " +
"where username=?");
这有一个问题,你数据库中的密码可能是一种加密方式加密过的,而用户传递的是明文,比较的时候需要进行加密处理,springsecurity也提供了相应的功能:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username, password, enabled from Users " +
"where username=?")
.authoritiesByUsernameQuery("select username, authority from UserAuthorities " +
"where username=?")
.passwordEncoder(new StandardPasswordEncoder("53cr3t");
passwordEncoder方法传递的是PasswordEncoder接口的实现,其默认提供了一些实现,如果都不满足,你可以实现这个接口:
BCryptPasswordEncoder
NoOpPasswordEncoder
Pbkdf2PasswordEncoder
SCryptPasswordEncoder
StandardPasswordEncoder(SHA-256)
3.基于LDAP
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication()
.userSearchBase("ou=people")
.userSearchFilter("(uid={0})")
.groupSearchBase("ou=groups")
.groupSearchFilter("member={0}")
.passwordCompare()
.passwordEncoder(new BCryptPasswordEncoder())
.passwordAttribute("passcode")
.contextSource()
.root("dc=tacocloud,dc=com")
.ldif("classpath:users.ldif");
4.用户自定义方式(最常用)
首先,你需要一个用户实体类,它实现UserDetails接口,实现这个接口的目的是为框架提供更多的信息,你可以把它看作框架使用的实体类:
@Data
public class User implements UserDetails {
private Long id;
private String username;
private String password;
private String fullname;
private String city;
private String phoneNumber;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
@Override
public boolean isAccountNonExpired() {
return false;
}
@Override
public boolean isAccountNonLocked() {
return false;
}
@Override
public boolean isCredentialsNonExpired() {
return false;
}
@Override
public boolean isEnabled() {
return false;
}
}
有了实体类,你还需要Service逻辑层,springsecurity提供了UserDetailsService接口,见名知意,你只要通过loadUserByUsername返回一个UserDetails对象就成,无论是基于文件、基于数据库、还是基于LDAP,剩下的对比判断交个框架完成:
@Service
public class UserService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
return null;
}
}
最后,进行应用:
@Autowired
private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder encoder() {
return new StandardPasswordEncoder("53cr3t");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(encoder());
}
配置认证路径
知道了如何认证,但现在有几个问题,比如,用户登录页面就不需要认证,可以用configure(HttpSecurity http)对认证路径进行配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
}
你可以通过这个方法,实现以下功能:
在提供接口服务前,判断请求必须满足某些条件
配置登录页面
允许用户注销登录
跨站点伪造请求防护
1.保护请求
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/design", "/orders").hasRole("ROLE_USER")
.antMatchers(“/”, "/**").permitAll();
}
要注意其顺序,除了hasRole和permitAll还有其它访问认证方法:
方法 | 作用 |
---|---|
access(String) | 如果给定的SpEL表达式的计算结果为true,则允许访问 |
anonymous() | 允许访问匿名用户 |
authenticated() | 允许访问经过身份验证的用户 |
denyAll() | 无条件拒绝访问 |
fullyAuthenticated() | 如果用户完全通过身份验证,则允许访问 |
hasAnyAuthority(String...) | 如果用户具有任何给定权限,则允许访问 |
hasAnyRole(String...) | 如果用户具有任何给定角色,则允许访问 |
hasAuthority(String) | 如果用户具有给定权限,则允许访问 |
hasIpAddress(String) | 如果请求来自给定的IP地址,则允许访问 |
hasRole(String) | 如果用户具有给定角色,则允许访问 |
not() | 否定任何其他访问方法的影响 |
permitAll() | 允许无条件访问 |
rememberMe() | 允许通过remember-me进行身份验证的用户访问 |
大部分方法是为特定方式准备的,但是access(String)可以使用SpEL进一些特殊的设置,但其中很大一部分也和上面的方法相同:
表达式 | 作用 |
---|---|
authentication | 用户的身份验证对象 |
denyAll | 始终评估为false |
hasAnyRole(list of roles) | 如果用户具有任何给定角色,则为true |
hasRole(role) | 如果用户具有给定角色,则为true |
hasIpAddress(IP address) | 如果请求来自给定的IP地址,则为true |
isAnonymous() | 如果用户是匿名用户,则为true |
isAuthenticated() | 如果用户已通过身份验证,则为true |
isFullyAuthenticated() | 如果用户已完全通过身份验证,则为true(未通过remember-me进行身份验证) |
isRememberMe() | 如果用户通过remember-me进行身份验证,则为true |
permitAll | 始终评估为true |
principal | 用户的主要对象 |
示例:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/design", "/orders").access("hasRole('ROLE_USER')")
.antMatchers(“/”, "/**").access("permitAll");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/design", "/orders").access("hasRole('ROLE_USER') && " +
"T(java.util.Calendar).getInstance().get("+"T(java.util.Calendar).DAY_OF_WEEK) == " + "T(java.util.Calendar).TUESDAY")
.antMatchers(“/”, "/**").access("permitAll");
}
2.配置登录页面
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/design", "/orders").access("hasRole('ROLE_USER')")
.antMatchers(“/”, "/**").access("permitAll")
.and()
.formLogin()
.loginPage("/login");
}
// 增加视图处理器
@Overridepublic void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
registry.addViewController("/login");
}
默认情况下,希望传递的是username和password,当然你可以修改:
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/authenticate")
.usernameParameter("user")
.passwordParameter("pwd")
也可修改默认登录成功的页面:
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/design")
3.配置登出
.and()
.logout()
.logoutSuccessUrl("/")
4.csrf攻击
springsecurity默认开启了防止csrf攻击,你只需要在传递的时候加上:
<input type="hidden" name="_csrf" th:value="${_csrf.token}"/>
当然,你也可以关闭,但是不建议这样做:
.and()
.csrf()
.disable()
知道用户是谁
仅仅控制用户登录有时候是不够的,你可能还想在程序的其它地方获取已经登录的用户信息,有几种方式可以做到:
将Principal对象注入控制器方法
将Authentication对象注入控制器方法
使用SecurityContextHolder获取安全上下文
使用@AuthenticationPrincipal注解方法
1.将Principal对象注入控制器方法
@PostMappingpublic String processOrder(@Valid Order order, Errors errors,SessionStatus sessionStatus,Principal principal) {
...
User user = userRepository.findByUsername(principal.getName());
order.setUser(user);
...
}
2.将Authentication对象注入控制器方法
@PostMappingpublic String processOrder(@Valid Order order, Errors errors, SessionStatus sessionStatus, Authentication authentication) {
...
User user = (User) authentication.getPrincipal();
order.setUser(user);
...
}
3.使用SecurityContextHolder获取安全上下文
Authentication authentication =
SecurityContextHolder.getContext().getAuthentication();
User user = (User) authentication.getPrincipal();
4.使用@AuthenticationPrincipal注解方法
@PostMappingpublic String processOrder(@Valid Order order, Errors errors,SessionStatus sessionStatus, @AuthenticationPrincipal User user) {
if (errors.hasErrors()) {
return "orderForm";
}
order.setUser(user);
orderRepo.save(order);
sessionStatus.setComplete();
return "redirect:/";
}
来源:https://www.cnblogs.com/NameZZH/p/11457146.html


猜你喜欢
- 本文为大家分享了NancyFx框架检测任务管理器的具体方法,供大家参考,具体内容如下先建一个空的项目和之前的NancyFx系列一样的步骤然后
- 使用AS创建ADIL文件时AS会在main文件夹下给我们生成一个aidl文件夹和一个相同包名的包,通常我们会把所有和ADIL相关的类或文件放
- 本文实例为大家分享了java实现捕鱼达人游戏的具体代码,供大家参考,具体内容如下效果图如下:源代码分享:测试类:package game;i
- 目录无SpringMVC全局异常时的流程图SpringMVC全局异常流程图其实是一个ModelAndView对象配置文件applicatio
- 前言convert 叫强制转换,可以是其他类型。最近在工作中遇到一个问题,需要将字符串形式的数值转换回数值,很正常的要求吧。却遇到了问题,下
- 1、CountDownLatch:一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行。2、ThreadPoolE
- 本文实例讲述了C++求四个正整数最大公约数的方法。分享给大家供大家参考,具体如下:/** 作 者: 刘同宾* 完成日期:2012 年 11
- 1, * 的概念java里的 * 是动态拦截Action调用的对象,它提供了一种机制可以使开发者在一个Action执行的前后执行
- (一)什么是微服务网关后端写完所有的微服务之后,最终是要交给前端去调用。我们都知道每个微服务都有各自的端口号,如果前端直接通过IP加端口的方
- 将int数组转化为Integer数组这里使用java8的stream来进行转化,详细步骤如下所示://初始化int数组int[] nums
- 具体内容如下所示:Intent.ACTION_AIRPLANE_MODE_CHANGED;//关闭或打开飞行模式时的广播Intent.ACT
- 在使用jsoup爬取其他网站数据的时候,发现class是带空格的多选择,如果直接使用doc.getElementsByClass(“clas
- 不知道大家对千篇一律的404 Not Found的错误页面是否感到腻歪了?其实通过很简单的配置就能够让Spring MVC显示您自定义的40
- 一、概述Overview - LINQ to XML | Microsoft 官方文档LINQ to XMLLINQ to XML 是一种启
- 一,抽象的实现using System;using System.Collections.Generic;using System.Linq
- 今天做了一个java对象转Map的例子,执行的时候报错了,如下:Exception in thread "main" j
- Xamarin写Android程序时,通常要使用按中文首字母分组显示(如通讯录) 。于是需要被迫包含CJK,不过包含后包肯定是会变大的,于是
- 本文实例为大家分享了javaweb登录验证码的具体代码,供大家参考,具体内容如下使用:Controller:生成验证码@RequestMap
- android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,有必要对它进行深入的了解。 一个最简单的屏幕触
- 在 Android 手机中内置了一款高性能 webkit 内核浏览器, SDK 中封装为一个叫做 WebView 组件。 WebView 类