码云地址: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. 实验12.dhcp服务器实验

    实验12.dhcp服务器实验 测试DHCP服务的可用性 实验组 交换机配置 R1 interface GigabitEthernet0/0/0 ip address 192.168.1.1 255.2 ...

  2. QT学习:03 信号与槽

    --- title: framework-cpp-qt-03-信号与槽 EntryName: framework-cpp-qt-03-signal-slot date: 2020-04-09 13:5 ...

  3. java实现微信登录

    前言 上一篇做了php的微信登录,所以也总结一下Java的微信授权登录并获取用户信息这个功能的开发流程. 配置 配置什么的就不多说了,详细的配置可以直接前往我上一篇查看. https://www.cn ...

  4. 单芯片国产ARM+FPGA,复旦微FMQL20SM工业核心板正式发布!

  5. 从零开始带你上手体验Sermant自定义插件开发

    本文分享自华为云社区<Sermant自定义插件开发上手体验>,作者:华为云开源. 一.研究缘由 由于目前我们所处的行业是汽车行业,项目上进行云服务的迁移时使用到了Sermant中的相关插件 ...

  6. 使用flume将数据sink到kafka

    flume采集过程: #说明:案例是flume监听目录/home/hadoop/flume_kafka采集到kafka: 启动集群 启动kafka, 启动agent,flume-ng agent -c ...

  7. npm install及其目录结构

    npm install 安装包及其依赖.npm install: 默认情况下,安装package.json文件中列出的所有依赖.加-P或--production后,只安装dependencies列出的 ...

  8. ComfyUI进阶篇:ComfyUI核心节点(四)

    ComfyUI核心节点(四) 前言: 学习ComfyUI是一场持久战.当你掌握了ComfyUI的安装和运行之后,会发现大量五花八门的节点.面对各种各样的工作流和复杂的节点种类,可能会让人感到不知所措. ...

  9. VUEX - 手稿

  10. JavaSE 计算2个List集合中的交集、差集、并集、去重并集

    VideoOrder.java 重写里面的equals和hashCode方法 class VideoOrder { private int price; private String title; p ...