码云地址:https://gitee.com/lpxs/lp-springcloud.git
有问题可以多沟通:136358344@qq.com。

刚开始用springboot1.5集成oauth2没问题,现在升级成springboot2.1踩了不少坑,下面列举下:

问题一

Possible CSRF detected - state parameter was required but no state could be found

客户端代码

@EnableOAuth2Sso
@Configuration
public class UiSecurityConfig extends WebSecurityConfigurerAdapter { @Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**")
.permitAll()
.anyRequest()
.authenticated();
} }

在获取到code后一直停留在登陆页面上

在网上找了下有以下方案:
1、配置server.servlet.session.cookie.name=UPSESSIONID
但是这个试了没效果
2、设置code策略authCodeProvider.setStateMandatory(false);
这里改动了很多代码

@Configuration
@EnableOAuth2Client
@EnableGlobalMethodSecurity(prePostEnabled=true)//开启@PreAuthorize注解
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private OAuth2ClientContext oauth2ClientContext;
@Override
protected void configure(HttpSecurity http) throws Exception {
// // @formatter:off
http.authorizeRequests()
.anyRequest().authenticated().and()
.formLogin().loginPage("/login").permitAll().and()
.exceptionHandling().and()
.logout().logoutSuccessUrl("/login").permitAll()
.and().headers().frameOptions().sameOrigin()
.and().csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
.addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);//这里需要配置在basic前
}
@Bean
public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(filter);
registration.setOrder(-100);
return registration;
} @Bean
@ConfigurationProperties("security.oauth2")
public ClientResources trina() {
return new ClientResources();
} private Filter ssoFilter() {
CompositeFilter filter = new CompositeFilter();
List<filter> filters = new ArrayList<filter>();
filters.add(ssoFilter(trina(), "/login"));
filter.setFilters(filters);
return filter;
}
private Filter ssoFilter(ClientResources client, String path) {
OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = new OAuth2ClientAuthenticationProcessingFilter(
path);
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client.getClient(), this.oauth2ClientContext);
oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);
AuthorizationCodeAccessTokenProvider authCodeProvider = new AuthorizationCodeAccessTokenProvider();
authCodeProvider.setStateMandatory(false);
AccessTokenProviderChain provider = new AccessTokenProviderChain(
Arrays.asList(authCodeProvider));
oAuth2RestTemplate.setAccessTokenProvider(provider);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(client.getResource().getUserInfoUri(),
client.getClient().getClientId());
tokenServices.setRestTemplate(oAuth2RestTemplate);
oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
return oAuth2ClientAuthenticationFilter;
} } class ClientResources { @NestedConfigurationProperty
private AuthorizationCodeResourceDetails client = new AuthorizationCodeResourceDetails(); @NestedConfigurationProperty
private ResourceServerProperties resource = new ResourceServerProperties(); public AuthorizationCodeResourceDetails getClient() {
return client;
} public ResourceServerProperties getResource() {
return resource;
}
}

修改后访问呢连接,登陆后成功跳转到指定页面。</filter></filter>

springboot2集成oauth2坑一(Possible CSRF detected - state parameter was required but no state could )的更多相关文章

  1. Spring Security Oauth2 : Possible CSRF detected

    Spring Security Oauth2 : Possible CSRF detected 使用Spring Security 作为 Oauth2 授权服务器时,在授权服务器登录授权后,重定向到客 ...

  2. Activiti(二) springBoot2集成activiti,集成activiti在线设计器

    摘要 本篇随笔主要记录springBoot2集成activiti流程引擎,并且嵌入activiti的在线设计器,可以通过浏览器直接编辑出我们需要的流程,不需要通过eclipse或者IDEA的actiB ...

  3. springboot 集成oauth2

    未实现.首先实现spring security. 1. 关于oauth2 隐隐觉得集成oauth2,用好它是一个不太简单的事儿,需要对oauth2了解一番. oauth2比较好的参考,都是别人原创文章 ...

  4. SpringBoot1.x升级SpringBoot2.x踩坑之文件上传大小限制

    SpringBoot1.x升级SpringBoot2.x踩坑之文件上传大小限制 前言 LZ最近升级SpringBoo框架到2.1.6,踩了一些坑,这里介绍的是文件上传大小限制. 升级前 #文件上传配置 ...

  5. springboot2集成pagehelper

    springboot2集成pagehelper超级简单,本示例直接抄袭官方示例,仅将数据库由H2改成MySQL而已. 1. pom.xml <?xml version="1.0&quo ...

  6. UidGenerator springboot2集成篇

    uid-generator 官网集成文档: https://github.com/baidu/uid-generator/blob/master/README.zh_cn.md 由于并没有提供spri ...

  7. SpringBoot2 集成三种连接池 c3p0 hikari druid

    Hikari 1.首先集成 hikari springboot默认集成,只需要简单的配置即可 1.1 首先导入包 <dependency> <groupId>com.zaxxe ...

  8. Spring Boot Restful WebAPI集成 OAuth2

    系统采用前后端分离的架构,采用OAuth2协议是很自然的事情. 下面开始实战,主要依赖以下两个组件: <dependency> <groupId>org.springframe ...

  9. SpringBoot2 集成日志,复杂业务下的自定义实现

    本文源码:GitHub·点这里 || GitEE·点这里 一.日志体系集成 1.日志管理 在系统的开发中,最关键的一个组件工具就是日志,日志打印方便问题排查,或者生产事故回溯,日志记录用来监控并分析系 ...

  10. SpringBoot2 整合OAuth2组件,模拟第三方授权访问

    本文源码:GitHub·点这里 || GitEE·点这里 一.模式描述 授权服务 验证第三方服务的身份,验证邮箱用户的身份,记录和管理认证Token,为资源服务器提供Token校验.场景:第三方网站借 ...

随机推荐

  1. 初识python day1记录

    程序语言中的分类 在程序中有分为高级语言Java python go与低级语言C 汇编,每种语言都有自己的规则,但是最终目的都是给计算机识别的,所以他的底层肯定是一些二进制010101,像java/p ...

  2. Nuxt3 的生命周期和钩子函数(七)

    title: Nuxt3 的生命周期和钩子函数(七) date: 2024/6/30 updated: 2024/6/30 author: cmdragon excerpt: 摘要:文章阐述了Nuxt ...

  3. Ubuntu 22.04扩容LVM空间

    今天为了编译ThingsBoard的源代码,发现原来给虚拟机分配的40个G不够用了.于是乎在VMWare Workstation中扩容了40G的磁盘空间.但是此时lvm是不会自动扩容的,因此我们需要手 ...

  4. 面试官:JVM调优,主要针对是哪一个区域?JVM内存结构是怎样的?

    作为一个Java程序员,在日常的开发中,不必像C/C++程序员那样,为每一个内存的分配而操心,JVM会替我们进行自动的内存分配和回收,方便我们开发.但是一旦发生内存泄漏或者内存溢出,如果对Java内存 ...

  5. fpga是什么

    FPGA(Field Programmable Gate Array) 现场可编程门阵列

  6. C# 日期帮助类

    using System; using System.Data; namespace Erp.Ship.Tool { [Serializable] public enum DateInterval { ...

  7. 探究kubernetes 探针参数periodSeconds和timeoutSeconds

    探究kubernetes 探针参数 periodSeconds和timeoutSeconds 问题起源 kubernetes probes的配置中有两个容易混淆的参数,periodSeconds和ti ...

  8. clickhouse节点重做(节点替换)

    测试验证环境: docker容器化部署的4节点2分片和2副本(centos7+clickhouse22.1.3) 172.17.0.6 clickhouse01172.17.0.7 clickhous ...

  9. Django 自带忘记密码,密码重置功能

    registration/password_reset_form.html: 重置密码表单模板 registration/password_reset_email.html: 发送重置密码邮件模板 r ...

  10. django redirect_authenticated_user

    redirect_authenticated_user是在Web开发框架(如Django)中常见的设置,用于控制身份验证重定向的行为. 当启用redirect_authenticated_user时, ...