gpt4 book ai didi

Spring框架 "A redirect is required to get the users approval"

转载 作者:行者123 更新时间:2023-12-02 22:53:12 25 4
gpt4 key购买 nike

我的 Spring Web 应用程序有问题。我想使用 web 应用程序访问 google(日历)api,因此我必须向 api 验证自己的身份并授予对日历的访问权限。

但实际的问题是我收到错误org.springframework.security.oauth2.client.resource.UserRedirectRequiredException:需要重定向才能获得用户批准

web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring-security.xml</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml, /WEB-INF/spring-security.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd">

<oauth:client id="oauth2AuthenticationClientFilter" />

<oauth:resource id="oauth-resource"
client-authentication-scheme="form" type="authorization_code"
access-token-uri="https://accounts.google.com/o/oauth2/token"
user-authorization-uri="https://accounts.google.com/o/oauth2/auth"
client-id="CLIENT-ID"
client-secret="CLIENT-SECRET" scope="https://www.googleapis.com/auth/calendar"
pre-established-redirect-uri="http://localhost:8080/" />

<oauth:rest-template id="oauth-rest-template"
resource="oauth-resource" />
</beans:beans>

Controller

    @Autowired
@Qualifier("oauth-rest-template")
private OAuth2RestTemplate oauth2RestTemplate;


/**
* Simply selects the home view to render by returning its name.
* @throws Exception
*/
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String home(Locale locale, Model model) throws Exception {


String dataUri = "https://www.googleapis.com/calendar/v3/calendars/sebastian.heckmann%40googlemail.com";

Calendar result = oauth2RestTemplate.getForObject(dataUri, Calendar.class);

// ...

return "home";
}

如果您需要更多代码,请告诉我。我是 Spring(安全)新手

最佳答案

我相信您正在使用 Spring Security,并且必须在 web.xml 中定义 DelegatingFilterProxy 过滤器。

您的代码片段没有描述安全配置,您需要执行以下操作才能使其正常工作:-

  1. 在 < sec:http> 标记内,定义自定义过滤器:-

    < 安全:自定义过滤器 ref="oauth2AuthenticationClientFilter" 之后=“EXCEPTION_TRANSLATION_FILTER”/>

  2. 将监听器添加到 web.xml

    org.springframework.web.context.request.RequestContextListener

关于Spring框架 "A redirect is required to get the users approval",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20376400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com