首页 > 其他分享 >跨域问题(普通跨域和springsecurity跨域)

跨域问题(普通跨域和springsecurity跨域)

时间:2024-10-01 17:49:45浏览次数:1  
标签:http 跨域 springframework springsecurity 普通 import 允许 public

跨域问题老生常谈了, 前后端分离项目会用到,浏览器端的请求需要ip,协议,端口完全一直否则浏览器会拦截

普通:

package com.example.openai.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
 public class CorsConfig implements WebMvcConfigurer {
 @Override
 public void addCorsMappings(CorsRegistry registry) {
           // 设置允许跨域的路径
  registry.addMapping("/**")
          // 设置允许跨域请求的域名
          .allowedOriginPatterns("*")
          // 是否允许cookie
          .allowCredentials(true)
          // 设置允许的请求方式
          .allowedMethods("GET", "POST", "DELETE", "PUT")
          // 设置允许的header属性
          .allowedHeaders("*")
          // 跨域允许时间
          .maxAge(3600);
 }
 }

springsecurity

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private  JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Autowired
    private AccessDeniedExceptionImpl accessDeniedException;
    @Autowired
    private Renzheng renzheng;
    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new  BCryptPasswordEncoder();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext

                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 对于登录接口 允许匿名访问
                .antMatchers("/user/login").anonymous()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        http.exceptionHandling()
                //认证处理
                .authenticationEntryPoint(renzheng)
                //异常处理
                .accessDeniedHandler(accessDeniedException);
        http.cors();
    }

设置http.cors()即可

标签:http,跨域,springframework,springsecurity,普通,import,允许,public
From: https://www.cnblogs.com/fubai/p/18443020

相关文章

  • P3369 【模板】普通平衡树
    直接抄WIDA的pbds板子#include<bits/stdc++.h>#include<ext/pb_ds/assoc_container.hpp>usingnamespace__gnu_pbds;usingnamespacestd;typedefpair<int,int>V;tree<V,null_type,less<V>,rb_tree_tag,tree_order_statistics_node_updat......
  • SpringSecurity自定义失败处理
    认证异常处理@ComponentpublicclassRenzhengimplementsAuthenticationEntryPoint{@Overridepublicvoidcommence(HttpServletRequesthttpServletRequest,HttpServletResponsehttpServletResponse,AuthenticationExceptione)throwsIOException,Servlet......
  • 常见问题解决 --- 如何解决CROS跨域问题
    问题原因:前后端不是一个服务导致的浏览器禁止访问的安全问题。比如前端部署在http://x.x.x.x:8888,后端部署在http://x.x.x.x:9999,由于端口不一致,浏览器安全起见不允许一个web页面有不同ip或端口的地址发送出流量。在开发者工具可以看出CROS错误。解决办法:关闭浏览器安全策......
  • html2canvas图片跨域问题
    需求:页面有个弹窗,弹窗内部有网站logo、表格、第三方的图片等内容,点击打印按钮,将弹窗区域内容下载至本地安装依赖pnpmaddhtml2canvas引入importhtml2canvasfrom'html2canvas'使用<template>...<button@click="handlePrint()">打印</button></template><s......
  • pbootcms后台的百度普通收录token怎么填写?怎么获得?
    在PbootCMS中配置百度普通收录Token的步骤如下:1.获取百度普通收录Token访问百度搜索资源平台访问百度搜索资源平台:https://ziyuan.baidu.com/如果没有账号,先注册一个账号。进入用户中心登录后点击“用户中心”。进入“站点管理”。添加网站点击“添加网......
  • java-快速将普通main类变为javafx类,并加载自定义fxml
    java-快速将普通main类变为javafx类,并加载自定义fxml前提步骤1.普通类继承Application2.实现main方法3.写一个controller4.写一个fxml文件5.写start方法加载fxml6.具体代码7.运行即可前提使用自带javafx的jdk,这里使用的是jdk1.834,当然你可以使用其他的可行......
  • 接上文实现SpringSecurity,拦截器的实现
    实现拦截器有图片可知,在上篇文章我们重写了UserDetailsManager,现在我们来进行之后的操作在UserDetailsManager中我们可以调动数据库去进行一个账号密码的校验之后我们这样设置拦截器进行一个token获取存储在usernamePasswordAuthenticationFilter这一层中,有,则存储在Secur......
  • 普通人如何开启副业之路:解锁群资源的无限潜力
    在当今这个信息爆炸的时代,普通人想要通过副业获得稳定的收入,已不再局限于传统的线下模式或单一技能输出。随着互联网的普及,特别是社交媒体和各类社群平台的兴起,利用“群”这一独特的资源,成为了许多人实现副业增收的新途径。本文将探讨如何巧妙地运用“群资源”、“找群”、“本......
  • 7、集成SpringSecurity安全框架---定义统一的响应对象类
    自定义/***@Description:TODO:定义统一的响应对象类*/@Data@AllArgsConstructor@NoArgsConstructorpublicclassResultVO<T>implementsSerializable{privatestaticfinallongserialVersionUID=-2548645345465031121L;privateIntegercode;pr......
  • 8、集成SpringSecurity安全框架---登录请求放行配置
    @ConfigurationpublicclassSecurityConfig{//创建BCryptPasswordEncoder注入容器,密码加密@BeanpublicPasswordEncoderpasswordEncoder(){returnnewBCryptPasswordEncoder();}//登录时调用一次AuthenticationManager.authenticat......