Security的一些配置
package com.example.demo.config; import com.example.demo.Service.UserDetailsServiceImpl;
import com.example.demo.filter.JwtAuthencationTokenFilter;
import com.example.demo.pojo.ResponseResult;
import com.example.demo.pojo.ResponseStatusCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter; @EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean
public JwtAuthencationTokenFilter jwtAuthencationTokenFilter(){
return new JwtAuthencationTokenFilter();
} @Autowired
private BCryptPasswordEncoder encoder; @Autowired
private UserDetailsServiceImpl userDetailsService; /**************************************************************
一,验证
**************************************************************/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(encoder);
} /**************************************************************
二,授权
**************************************************************/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authenticationProvider(authenticationProvider())
.httpBasic()
//未登录时 , 自定义响应结果
.authenticationEntryPoint((request, response, ex) -> {
customResponse(response, ResponseStatusCode.NO_Login,null);
}); http.authorizeRequests()
.antMatchers("/common").hasRole("common")
.antMatchers("/vip").hasRole("vip")
.antMatchers("/").authenticated(); //将jwt登录授权的拦截器 => 添加到用户验证之前
http.addFilterBefore(jwtAuthencationTokenFilter(), UsernamePasswordAuthenticationFilter.class); /*
添加自定义 未授权和未授权的结果返回
*/
http.exceptionHandling()
.accessDeniedHandler((request, response, ex) -> {
customResponse(response, ResponseStatusCode.NO_AUTHORITY,null);
})
.authenticationEntryPoint((request, response, ex) -> {
customResponse(response, ResponseStatusCode.NO_Login,null);
}); //基于token,所有不需要csrf
http.csrf().disable();
// 基于token,所以不需要session
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// 缓存管理,暂时用不到
http.headers().cacheControl(); } @Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
//对默认的UserDetailsService进行覆盖
authenticationProvider.setUserDetailsService(userDetailsService);
authenticationProvider.setPasswordEncoder(encoder);
return authenticationProvider;
} @Autowired
private ObjectMapper objectMapper; // 自定义返回结果
private void customResponse(HttpServletResponse response, ResponseStatusCode code,Object data) throws IOException {
response.setContentType("application/json;charset=utf-8");
response.setStatus(code.getCode());
PrintWriter out = response.getWriter();
out.write(objectMapper.writeValueAsString(new ResponseResult<>(code, data)));
out.flush();
out.close();
} }
Security的一些配置的更多相关文章
- CAS Spring Security 3 整合配置(转)
一般来说, Web 应用的安全性包括用户认证( Authentication )和用户授权( Authorization )两个部分.用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否 ...
- Spring Security基于Java配置
Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...
- springboot对security的后端配置
一.Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring ...
- spring boot rest 接口集成 spring security(2) - JWT配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- Spring Security(三) —— 核心配置解读
摘要: 原创出处 https://www.cnkirito.moe/spring-security-3/ 「老徐」欢迎转载,保留摘要,谢谢! 3 核心配置解读 上一篇文章<Spring Secu ...
- spring security 3.2 配置详解(结合数据库)
没事就来了解下spring security.网上找了很多资料.有过时的,也有不是很全面的.各种问题也算是让我碰了个遍.这样吧.我先把整个流程写下来,之后在各个易混点分析吧. 1.建立几个必要的页面. ...
- 【JavaEE】SSH+Spring Security基础上配置AOP+log4j
Spring Oauth2大多数情况下还是用不到的,主要使用的还是Spring+SpringMVC+Hibernate,有时候加上SpringSecurity,因此,本文及以后的文章的example中 ...
- Spring Security Oauth2 的配置
使用oauth2保护你的应用,可以分为简易的分为三个步骤 配置资源服务器 配置认证服务器 配置spring security 前两点是oauth2的主体内容,但前面我已经描述过了,spring sec ...
- Spring Security之动态配置资源权限
在Spring Security中实现通过数据库动态配置url资源权限,需要通过配置验证过滤器来实现资源权限的加载.验证.系统启动时,到数据库加载系统资源权限列表,当有请求访问时,通过对比系统资源权限 ...
- spring security实现动态配置url权限的两种方法
缘起 标准的RABC, 权限需要支持动态配置,spring security默认是在代码里约定好权限,真实的业务场景通常需要可以支持动态配置角色访问权限,即在运行时去配置url对应的访问角色. 基于s ...
随机推荐
- 华企盾DSC邮件服务器测试连接提示Server has closed the connection(端口不对)
解决方法:邮件服务器端口填错了,应该是smtp.126.com:s465,或者smtp.126.com:s587 其他邮箱同理.
- Java 中时间对象的序列化
在 Java 应用程序中,时间对象是使用地比较频繁的对象,比如,记录某一条数据的修改时间,用户的登录时间等应用场景.在传统的 Java 编程中,大部分的程序员都会选择使用 java.uti.Date ...
- 原生JavaScript 与 jQuery 执行Ajax请求
原生JavaScript和jQuery都可以用来执行Ajax请求,以下是它们的基本实现方式的比较: 原生JavaScript实现Ajax请求: var xhr = new XMLHttpRequest ...
- 深入理解JavaScript堆栈、事件循环、执行上下文和作用域以及闭包
1. 堆栈 在JavaScript中,内存堆是内存分配的地方,调用栈是代码执行的地方. 原始类型的保存方式:在变量中保存的是值本身,所以原始类型也被称之为值类型. 对象类型的保存方式:在变量中保存的是 ...
- Windows10安装WSL2和Ubuntu的过程
因为在Windows10环境中安装了2个相同的包导致冲突,所以想到了通过WSL2+Docker的方式进行编程开发.因为Docker Desktop直接安装就行了,不做介绍.本文主要介绍WSL2和U ...
- 如何通过一个SDK轻松搞定人脸识别,拯救初入职场的程序猿
摘要:看一个SDK如何拯救初入职场的程序猿小Hi- [职场初体验] 时间过得真快,距离上次给小Hi安排"人脸识别"的开发任务(话接上期:[快速玩转华为云开发]小Hi拍了拍你,基于华 ...
- GaussDB(DWS)迁移:一种执行高效的TereData的marco迁移方案
摘要:提供一种执行高效的TereData的marco迁移方案. 本文分享自华为云社区<GaussDB(DWS)迁移 - teredata兼容 -- macro兼容 # [玩转PB级数仓Gauss ...
- 一文带你了解华为云GaussDB的五大黑科技
摘要:在华为开发者大会2021(HDC·Cloud)中,华为云数据库多位技术专家分享了GaussDB系列数据库全新5大黑科技. 4月24-26日,华为开发者大会2021(HDC·Cloud)在深圳隆重 ...
- 一起学习ML和DL中常用的几种loss函数
摘要:本篇内容和大家一起学习下机器学习和深度学习中常用到的几种loss函数. 本文分享自华为云社区<[MindSpore易点通]网络实战之交叉熵类Loss函数>,作者:Skytier . ...
- 6个步骤强化 CI/CD 安全
快速的数字化和越来越多的远程业务运营给开发人员带来了沉重的负担,他们不断面临着更快推出软件的压力.尽管CI/CD 加速了产品发布,但它容易受到网络安全问题的影响,例如代码损坏.安全配置错误和机密管理不 ...