Spring Boot OAuth2,Tomcat和nginx在认证后得到错误ERR_TOO_MANY_REDIRECTS [英] Spring Boot OAuth2, with Tomcat and nginx get error ERR_TOO_MANY_REDIRECTS after authenticate

查看:50
本文介绍了Spring Boot OAuth2,Tomcat和nginx在认证后得到错误ERR_TOO_MANY_REDIRECTS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring Boot 应用程序,它使用来自 WSO2 身份服务器的 OAuth2 身份验证.当我在 Spring Tool Suit 上运行应用程序时,它可以工作,所以我可以唱歌并使用我的网站.但是当我在 Tomcat(9.0) 上运行我的应用程序时,我尝试访问一个页面,并重定向到登录页面,当我尝试登录时,我收到错误 ERR_TOO_MANY_REDIRECTS

I have a Spring Boot application, it use OAuth2 authentication from WSO2 Identity Server. When I run the aplication on Spring Tool Suit, it works, so i can sing in and use my web site. But when I run my application on Tomcat(9.0), I try access a page, and redirect to login page, and when i try to sign in, I get the error ERR_TOO_MANY_REDIRECTS

错误示例: 当我的 Spring Boot 应用程序在 Tomcat 上运行时,我尝试访问 html 页面:https://domain/chat/example.html

Error Example: When my spring boot app is runing on Tomcat, and I try to access the html page: https://domain/chat/example.html

如果用户未通过身份验证,重定向到登录页面 WSO2 身份服务器:https://domain/is/authenticationendpoint/login.do

if the user was not authenticated, redirects to login page WSO2 Identity Server: https://domain/is/authenticationendpoint/login.do

登录后页面重定向到下面的url,没有重定向到url(https://domain/chat/example.html)

after login, the page redirects to the urls below, and does not redirect to url(https://domain/chat/example.html)

  1. https://domain/is/oauth2/authorize
  2. https://domain/chat/oauth2/authorization/wso2
  3. https://domain/chat/login/oauth2/code/wso2
  4. https://domain/chat/login

这些页面返回错误ERR_TOO_MANY_REDIRECTS.

用户可以进行身份​​验证,但应用程序重定向并进入导致错误的循环,该循环位于 url 1、2、3、4 之间.

A user can authenticate, but the application redirect and go to a loop that causes the error, the loop is between the urls 1,2,3,4.

Tomcat 日志

Spring Boot 配置:

登录控制器.java

@Controller
public class LoginController {

    @GetMapping("/oauth-login")
    public String getLoginPage(Model model) {

        return "redirect:/oauth2/authorization/wso2";
    }
}

ConfigSecurity.java

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)//abilitar seguranca nos metodos
public class ConfigSecurity extends WebSecurityConfigurerAdapter {
    
   protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/oauth-login")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .oauth2Login().loginPage("/oauth-login")
                .and()
                .logout().logoutUrl("/applogout");

    }
}

application.properties

server.port=8443

spring.security.oauth2.client.registration.wso2.client-name=WSO2 Identity Server
spring.security.oauth2.client.registration.wso2.client-id=asdasd
spring.security.oauth2.client.registration.wso2.client-secret=asdasd
spring.security.oauth2.client.registration.wso2.redirect-uri=https://domain/chat/login/oauth2/code/wso2
spring.security.oauth2.client.registration.wso2.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.wso2.scope=openid

#Identity Server Properties
spring.security.oauth2.client.provider.wso2.authorization-uri=https://domain/is/oauth2/authorize
spring.security.oauth2.client.provider.wso2.token-uri=https://domain/is/oauth2/token
spring.security.oauth2.client.provider.wso2.user-info-uri=https://domain/is/oauth2/userinfo
spring.security.oauth2.client.provider.wso2.jwk-set-uri=https://domain/is/oauth2/jwks

这是我的 git:https://github.com/Mingato/Root2

我遵循了教程:https://medium.com/@piraveenaparalogarajah/secure-your-spring-boot-application-with-wso2-identity-server-8140af8aa30b

当我运行 .jar 文件时它可以工作,但当我在 tomcat 上运行 .war 文件时它不起作用.

When I run a .jar file it works but when I run a .war file on tomcat it does not work.

推荐答案

经过多次研究,我发现了我的错误.我的配置是正确的,但是当我在 Tomcat 上运行我的 Spring boot 应用程序时,我必须配置我的应用程序才能在它上面运行,但是还有另一种方式,我以最简单的方式运行我的应用程序,我生成 .jat 并执行下面的命令

After many researchs, i found my mistake. My Configurations is right, but when i run my Spring boot app on Tomcat, i have to configure my application to run on it, but there are another way, I run my app in the easiest way, I generate the .jat and execute with the command bellow

  java -jar myapp.jar

所以我删除了 Tomcat 服务器来部署我的 Spring Boot 应用程序.

So I remove the Tomcat Server to deploy my spring boot applications.

这篇关于Spring Boot OAuth2,Tomcat和nginx在认证后得到错误ERR_TOO_MANY_REDIRECTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆