Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]JSON解析bug #1417

Closed
TsukasaHwan opened this issue Apr 27, 2023 · 5 comments
Closed

[BUG]JSON解析bug #1417

TsukasaHwan opened this issue Apr 27, 2023 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@TsukasaHwan
Copy link

环境信息

请填写以下信息:

  • JDK信息: [e.g.:Openjdk 1.8.0_361]
  • 版本信息:[e.g.:Fastjson2 2.0.29]

重现步骤

        Filter autoTypeBeforeHandler = JSONReader.autoTypeFilter(
                // 按需加上需要支持自动类型的类名前缀,范围越小越安全
                "org.springframework.security.core.authority.SimpleGrantedAuthority"
        );
        String attr = "{\"@type\":\"java.util.Collections$UnmodifiableMap\",\"java.security.Principal\":{\"@type\":\"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\",\"authenticated\":true,\"authorities\":[{\"@type\":\"org.springframework.security.core.authority.SimpleGrantedAuthority\",\"authority\":\"ROLE_ADMIN\"}],\"details\":{\"@type\":\"org.springframework.security.web.authentication.WebAuthenticationDetails\",\"remoteAddress\":\"127.0.0.1\",\"sessionId\":\"38220AEDC251E5CE7F45344BCA805C89\"},\"name\":\"user\",\"principal\":{\"@type\":\"org.springframework.security.core.userdetails.User\",\"accountNonExpired\":true,\"accountNonLocked\":true,\"authorities\":Set[{\"@type\":\"org.springframework.security.core.authority.SimpleGrantedAuthority\",\"authority\":\"ROLE_ADMIN\"}],\"credentialsNonExpired\":true,\"enabled\":true,\"username\":\"user\"}},\"org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest\":{\"@type\":\"org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest\",\"additionalParameters\":{\"@type\":\"java.util.Collections$UnmodifiableMap\"},\"attributes\":{\"@type\":\"java.util.Collections$UnmodifiableMap\"},\"authorizationRequestUri\":\"http://127.0.0.1:9000/oauth2/authorize?response_type=code&client_id=messaging-client&scope=message.read&redirect_uri=http://127.0.0.1:8000/login/oauth2/code/messaging-client-oidc\",\"authorizationUri\":\"http://127.0.0.1:9000/oauth2/authorize\",\"clientId\":\"messaging-client\",\"grantType\":{\"value\":\"authorization_code\"},\"redirectUri\":\"http://127.0.0.1:8000/login/oauth2/code/messaging-client-oidc\",\"responseType\":{\"value\":\"code\"},\"scopes\":Set[\"message.read\"]},\"state\":\"Ahvv87vejITg_FgjAtcGUKMw_G_aZIS1fbdD_mcgXFg=\"}";
        Map<String, Object> attrMap = JSON.parseObject(attr, Map.class, autoTypeBeforeHandler);
        OAuth2AuthorizationRequest o = (OAuth2AuthorizationRequest) attrMap.get(OAuth2AuthorizationRequest.class.getName());
        System.out.println(o);

期待的正确结果

期望序列化为@type中的对象

相关日志输出

Exception in thread "main" com.alibaba.fastjson2.JSONException: illegal input S, offset 629, character S, line 1, column 630, fastjson-version 2.0.29 {"@type":"java.util.Collections$UnmodifiableMap","java.security.Principal":{"@type":"org.springframework.security.authentication.UsernamePasswordAuthenticationToken","authenticated":true,"authorities":[{"@type":"org.springframework.security.core.authority.SimpleGrantedAuthority","authority":"ROLE_ADMIN"}],"details":{"@type":"org.springframework.security.web.authentication.WebAuthenticationDetails","remoteAddress":"127.0.0.1","sessionId":"38220AEDC251E5CE7F45344BCA805C89"},"name":"user","principal":{"@type":"org.springframework.security.core.userdetails.User","accountNonExpired":true,"accountNonLocked":true,"authorities":Set[{"@type":"org.springframework.security.core.authority.SimpleGrantedAuthority","authority":"ROLE_ADMIN"}],"credentialsNonExpired":true,"enabled":true,"username":"user"}},"org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest":{"@type":"org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest","additionalParameters":{"@type":"java.util.Collections$UnmodifiableMap"},"attributes":{"@type":"java.util.Collections$UnmodifiableMap"},"authorizationRequestUri":"http://127.0.0.1:9000/oauth2/authorize?response_type=code&client_id=messaging-client&scope=message.read&redirect_uri=http://127.0.0.1:8000/login/oauth2/code/messaging-client-oidc","authorizationUri":"http://127.0.0.1:9000/oauth2/authorize","clientId":"messaging-client","grantType":{"value":"authorization_code"},"redirectUri":"http://127.0.0.1:8000/login/oauth2/code/messaging-client-oidc","responseType":{"value":"code"},"scopes":Set["message.read"]},"state":"Ahvv87vejITg_FgjAtcGUKMw_G_aZIS1fbdD_mcgXFg="}
at com.alibaba.fastjson2.JSONReader.readObject(JSONReader.java:1799)
at com.alibaba.fastjson2.JSONReader.readObject(JSONReader.java:1771)
at com.alibaba.fastjson2.JSONReader.read(JSONReader.java:1598)
at com.alibaba.fastjson2.reader.ObjectReaderImplMap.readObject(ObjectReaderImplMap.java:380)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:686)
at org.example.web.ServerController.main(ServerController.java:87)

@TsukasaHwan TsukasaHwan added the bug Something isn't working label Apr 27, 2023
@TsukasaHwan
Copy link
Author

问题找到了,但是toJSONString的json是这个格式,为啥读取的时候报错呢"authorities":Set[{"@type":"org.springframework.security.core.authority.SimpleGrantedAuthority","authority":"ROLE_ADMIN"}]

@TsukasaHwan
Copy link
Author

1682591278533
authorities对象为这个

@wenshao wenshao added this to the 2.0.32 milestone May 13, 2023
@wenshao
Copy link
Member

wenshao commented May 13, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.32-SNAPSHOT/
问题已修复,请帮忙用2.0.32-SNAPSHOT版本验证。

@TsukasaHwan
Copy link
Author

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.32-SNAPSHOT/ 问题已修复,请帮忙用2.0.32-SNAPSHOT版本验证。

已验证,没问题了

@wenshao
Copy link
Member

wenshao commented May 16, 2023

@wenshao wenshao closed this as completed May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants