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 ...
随机推荐
- 支付宝沙箱支付-zfbsxzf
title: 支付宝沙箱支付 date: 2022-03-03 13:55:15.281 updated: 2022-03-10 16:00:42.331 url: https://www.yby6. ...
- WinRM服务应用及配置说明
一.什么是winRM服务 1.1.winRM服务介绍 Windows远程管理(WinRM)服务是Windows Server 2003 R2以上版本中一种新式的方便远程管理的服务.通过WinRM服务, ...
- 好玩、有趣的 Linux 命令学习神器 kmdr!
大家好,我是你们的民工哥. 所有学习Linux系统的初学者都知道,入门时除了简单的系统知识需要了解之外,其次,最重要的就是学习与理解Linux命令的用法与其应用场景. 所以,大家也会遇到这样的情况,不 ...
- 【WALT】调度与负载计算(未更新完)
[WALT]调度与负载计算 代码版本:Linux4.9 android-msm-crosshatch-4.9-android12 注:本文中的任务主要指进程. @ 目录 [WALT]调度与负载计算 一 ...
- 云小课 | DSC之数据水印,防止数据被盗用
阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说).深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云.更多精彩内容请单击此处. 摘要: 华为云数据安全中 ...
- web内容如何保护:如何有效地保护 HTML5 格式的视频内容?
文章资料还没有整理完,现在还不通顺,但是可以做笔记看 HTML是用于创建web页面和web应用程序的公开标准的标记语言.HTML文件基本上是纯文本文件,这意味着软件应用程序和普通人可以轻松地创建.读取 ...
- 单向数据流-从共享状态管理:flux/redux/vuex漫谈异步数据处理
不管是Vue,还是 React,都需要管理状态(state),比如组件之间都有共享状态的需要. 什么是共享状态? 比如一个组件需要使用另一个组件的状态,或者一个组件需要改变另一个组件的状态,都是共享状 ...
- 火山引擎A/B测试私有化实践
更多技术交流.求职机会.试用福利,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 作为一款面向 ToB 市场的产品--火山引擎 A/B 测试(DataTester)为了满足客户对数据安全 ...
- Solon 问答:项目如何直接添加 https 支持?
app.yml 添加两行配置即可: #设定SSL证书(支持:solon.boot.jdkhttp 或 solon.boot.jlhttp 或 solon.boot.jetty 或 solon.boot ...
- Hugging News #0113:DreamBooth 编程马拉松活动保姆级视频教程来了!
每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...