Springframework“需要重定向才能获得用户批准". [英] Springframework "A redirect is required to get the users approval"

查看:79
本文介绍了Springframework“需要重定向才能获得用户批准".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的春季网络应用程序有问题.我想使用Web应用程序访问google(日历)api,因此我必须向api进行身份验证并授予对日历的访问权限.

i have a problem with my spring web-app. I want to access the google (calendar) api with the webapp and thus i have to authenticate myself to the api and grant access to the calendar.

但是实际的问题是我得到了错误org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: A redirect is required to get the users approval

But the actual problem is that i got the error org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: A redirect is required to get the users approval

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>

控制器

    @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(Security)的新手

If you need more code, please let me know. I am new to Spring (Security)

推荐答案

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

I believe you are using Spring Security and must have defined DelegatingFilterProxy filter in your web.xml.

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

Your code snippet doesn't descrive the security configuration, you need to do following to get it working:-

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

  1. Inside your < sec:http> tag, define the custom filter:-

<安全性:自定义过滤器ref ="oauth2AuthenticationClientFilter" after ="EXCEPTION_TRANSLATION_FILTER"/>

< security:custom-filter ref="oauth2AuthenticationClientFilter" after="EXCEPTION_TRANSLATION_FILTER" />

将列表添加到web.xml

Add listner to web.xml

org.springframework.web.context.request.RequestContextListener

org.springframework.web.context.request.RequestContextListener

这篇关于Springframework“需要重定向才能获得用户批准".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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