Giter Site home page Giter Site logo

@OAuth2 注解可配置化 about forest HOT 5 CLOSED

dromara avatar dromara commented on May 17, 2024
@OAuth2 注解可配置化

from forest.

Comments (5)

houkunlin avatar houkunlin commented on May 17, 2024 1

@John-Chan
在前面的代码片段中,有两行

OAuth2.GrantType grantType = (OAuth2.GrantType) getAttribute(request, "grantType");
String grantTypeValue = grantType.getValue(getAttributeAsString(request, "grantTypeValue"));

得到的 OAuth2.GrantType grantType 这个枚举是使用在注解中配置的 grantType 信息,然后下一行有一句

grantType.getValue(getAttributeAsString(request, "grantTypeValue"))

会先从注解中拿到 grantTypeValue 的配置作为默认值传入 OAuth2.GrantType.getValue(defaultGrantTypeValue) 中拿到具体的 grantTypeValue 信息。

在 OAuth2.GrantType.getValue(defaultGrantTypeValue) 中的代码如下:

/**
 * 获取 GrantType 的实际请求参数值
 *
 * @param defaultValue 默认的一个参数值,这个参数传入的应该为 @OAuth2.grantTypeValue 的值
 * @return GrantType 实际请求值
 */
public String getValue(String defaultValue) {
    if (StringUtils.isBlank(defaultValue)) {
        // 当 @OAuth2.grantTypeValue 未设置值时,使用 @OAuth2.grantType 默认值
        return value;
    }
    return defaultValue;
}

因此使用 grantType = OAuth2.GrantType.PASSWORD 表示使用注解中的 username/passwrod 参数,然后再配合 grantTypeValue = 'CustomGrantTypeValue' 配置,把默认的 grantTypeValue = 'password' 强制覆盖为 'CustomGrantTypeValue'

from forest.

mySingleLive avatar mySingleLive commented on May 17, 2024

可以使用 grantTypeValue 属性

image

from forest.

John-Chan avatar John-Chan commented on May 17, 2024

多谢!

from forest.

John-Chan avatar John-Chan commented on May 17, 2024

我发现使用使用 grantTypeValue 属性后,发起获取token的请求时,就把用户名密码信息给丢了。

日志对比

POST https://XXX/v1/token HTTPS
	Body: client_id=XX&client_secret=XX&scope=openid%20email%20profile&grant_type=password
   
POST https://XXX/v1/token HTTPS
	Body: client_id=XX&client_secret=XX&scope=openid%20email%20profile&grant_type=password&username=XX%40163.com&password=XX

我看了下代码,发现

    private Map<String, Object> createRequestBody(String clientId, ForestRequest request, boolean fillAccount) {
        Map<String, Object> body = new LinkedHashMap<>();
        body.put("client_id", clientId);
        body.put("client_secret", getAttributeAsString(request, "clientSecret"));
        body.put("scope", getAttributeAsString(request, "scope"));

        OAuth2.GrantType grantType = (OAuth2.GrantType) getAttribute(request, "grantType");
        String grantTypeValue = grantType.getValue(getAttributeAsString(request, "grantTypeValue"));
        body.put("grant_type", grantTypeValue);

        if (fillAccount && grantType == OAuth2.GrantType.PASSWORD) {
            body.put("username", getAttributeAsString(request, "username"));
            body.put("password", getAttributeAsString(request, "password"));
        }
        return body;
    }

最后的判断逻辑里面并没有判断 grantTypeValue 的情况,我不确定是不是bug。所以 grantTypeValue 还是不能替代 grantType

from forest.

houkunlin avatar houkunlin commented on May 17, 2024

@John-Chan
当初设计如此,当 grantType == OAuth2.GrantType.PASSWORD 时会使用 注解中的 username/password 信息
当 grantType == OAuth2.GrantType.CLIENT_CREDENTIALS 时,表示是不需要 username/passwrod 的,

所以,假如你的请求是需要带上 username/passwrod 的,可以把 grantType 设置为 OAuth2.GrantType.PASSWORD, 然后设置 grantTypeValue 强制覆盖请求中的 grantType 信息

from forest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.