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的一些配置的更多相关文章

  1. CAS Spring Security 3 整合配置(转)

    一般来说, Web 应用的安全性包括用户认证( Authentication )和用户授权( Authorization )两个部分.用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否 ...

  2. Spring Security基于Java配置

    Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...

  3. springboot对security的后端配置

    一.Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring ...

  4. spring boot rest 接口集成 spring security(2) - JWT配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  5. Spring Security(三) —— 核心配置解读

    摘要: 原创出处 https://www.cnkirito.moe/spring-security-3/ 「老徐」欢迎转载,保留摘要,谢谢! 3 核心配置解读 上一篇文章<Spring Secu ...

  6. spring security 3.2 配置详解(结合数据库)

    没事就来了解下spring security.网上找了很多资料.有过时的,也有不是很全面的.各种问题也算是让我碰了个遍.这样吧.我先把整个流程写下来,之后在各个易混点分析吧. 1.建立几个必要的页面. ...

  7. 【JavaEE】SSH+Spring Security基础上配置AOP+log4j

    Spring Oauth2大多数情况下还是用不到的,主要使用的还是Spring+SpringMVC+Hibernate,有时候加上SpringSecurity,因此,本文及以后的文章的example中 ...

  8. Spring Security Oauth2 的配置

    使用oauth2保护你的应用,可以分为简易的分为三个步骤 配置资源服务器 配置认证服务器 配置spring security 前两点是oauth2的主体内容,但前面我已经描述过了,spring sec ...

  9. Spring Security之动态配置资源权限

    在Spring Security中实现通过数据库动态配置url资源权限,需要通过配置验证过滤器来实现资源权限的加载.验证.系统启动时,到数据库加载系统资源权限列表,当有请求访问时,通过对比系统资源权限 ...

  10. spring security实现动态配置url权限的两种方法

    缘起 标准的RABC, 权限需要支持动态配置,spring security默认是在代码里约定好权限,真实的业务场景通常需要可以支持动态配置角色访问权限,即在运行时去配置url对应的访问角色. 基于s ...

随机推荐

  1. 万界星空科技QMS质量管理系统

    QMS(Quality Management System)质量管理系统是五大基础系统之一,在工业企业中被广泛的应用,在质量策划.生产过程质量监督.体系审核和文档管理等业务上发挥着不可替代的作用. 一 ...

  2. HTML之元素相对页面(视口)左上角的绝对坐标

    目录 一. 绝对位置 二. 相对位置 一. 绝对位置 网页元素的绝对位置,指该元素的左上角相对于整张网页左上角的坐标.无论网页滚动条如何滚动,它都是不会变化的. // 获取元素的绝对位置坐标(相对于页 ...

  3. vue 2实战系列 —— 复习Vue

    复习Vue 近期需要接手 vue 2的项目,许久未写,语法有些陌生.本篇将较全面复习 vue 2. Tip: 项目是基于 ant-design-vue-pro ant-design-vue-pro 由 ...

  4. 太赞了!墙裂推荐这款网页版 Nginx 配置生成器,好用到爆!

    之前民工哥也给大家介绍过一款Nginx配置生成器:强大!Nginx 配置在线一键生成"神器",不太了解的人可以去看一看. 最近民工哥又发现一款好用的网页版开源工具,同样它的功能也是 ...

  5. DNS解析中CNAME和MX记录冲突

    转载:DNS中CNAME和MX记录的冲突 在DNS解析中,CNAME记录与其他记录往往是互斥的.最常见的是CNAME记录和MX记录的互斥.例如我们在http://example.com部署官网,通过C ...

  6. 国内AI大模型 VS chatgpt (20)-- H265/H264有何不同,h265有什么优势?

    H265/H264有何不同,h265有什么优势? 文心一言: H.264和H.265是两种不同的视频编码标准,它们在压缩质量和带宽需求方面有所不同. H.264是一种高效的视频压缩标准,它的编码单位是 ...

  7. Altas&Ranger快速入门

    Altas&Ranger快速入门 一.元数据 企业内部远行多种类型的数据库,有关系型数据库.非关系型数据库.图数据库.时序数据库等,常见有mysql.redis.Mongodb.oracle. ...

  8. 云图说 | 容器交付流水线ContainerOps,助力企业容器化转型

    摘要:华为云容器镜像服务SWR推出容器交付流水线(ContainerOps)可以帮助企业容器化转型. 容器交付流水线(ContainerOps)是华为云容器镜像服务(SWR)推出的面向从源代码到生产上 ...

  9. Python从0到1丨图像增强及运算:形态学开运算、闭运算和梯度运算

    摘要:本文主要介绍图像形态学处理,详细讲解了图像开运算.闭运算和梯度运算.数学形态学是一种应用于图像处理和模式识别领域的新方法,其基本思想是用具有一定形态的结构元素去量度和提取图像中对应形状以达到对图 ...

  10. 这项评测,华为云GaussDB(for MySQL)顺利通过

    摘要:近日,中国信息通信研究院(简称"中国信通院")公布了第十五批"可信数据库"评测结果.华为云GaussDB(for MySQL)凭借过硬的技术实力顺利通过& ...