Skip to content

Commit 7fa6695

Browse files
wzkriszrlw
andauthored
fix spring OAuth2 class serialize (#15414)
* fix spring OAuth2 class serialize * rollback cluster pom edit * format code * ensure compile normally if not import Spring-Authorization-Server * Update pom.xml --------- Co-authored-by: zrlw <zrlw@sina.com>
1 parent 1662c41 commit 7fa6695

13 files changed

Lines changed: 549 additions & 0 deletions

dubbo-plugin/dubbo-spring-security/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
<properties>
3131
<skip_maven_deploy>false</skip_maven_deploy>
32+
<spring.oauth2.server>1.5.1</spring.oauth2.server>
3233
</properties>
3334

3435
<dependencies>
@@ -65,6 +66,12 @@
6566
<optional>true</optional>
6667
</dependency>
6768

69+
<dependency>
70+
<groupId>org.springframework.security</groupId>
71+
<artifactId>spring-security-oauth2-authorization-server</artifactId>
72+
<version>${spring.oauth2.server}</version>
73+
<optional>true</optional>
74+
</dependency>
6875
<!-- spring security -->
6976

7077
<!-- jackson -->

dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/jackson/ObjectMapperCodec.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@
2121
import org.apache.dubbo.common.logger.LoggerFactory;
2222
import org.apache.dubbo.common.utils.ClassUtils;
2323
import org.apache.dubbo.common.utils.StringUtils;
24+
import org.apache.dubbo.spring.security.oauth2.OAuth2SecurityModule;
2425

2526
import java.nio.charset.StandardCharsets;
2627
import java.util.ArrayList;
2728
import java.util.List;
2829
import java.util.function.Consumer;
2930

31+
import com.fasterxml.jackson.databind.Module;
3032
import com.fasterxml.jackson.databind.ObjectMapper;
3133
import com.fasterxml.jackson.databind.module.SimpleModule;
3234
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
3335
import org.springframework.security.jackson2.CoreJackson2Module;
36+
import org.springframework.security.jackson2.SecurityJackson2Modules;
3437

3538
public class ObjectMapperCodec {
3639

@@ -101,6 +104,10 @@ public ObjectMapperCodec configureMapper(Consumer<ObjectMapper> objectMapperConf
101104
private void registerDefaultModule() {
102105
mapper.registerModule(new CoreJackson2Module());
103106
mapper.registerModule(new JavaTimeModule());
107+
mapper.registerModule(new OAuth2SecurityModule());
108+
List<Module> securityModules =
109+
SecurityJackson2Modules.getModules(this.getClass().getClassLoader());
110+
mapper.registerModules(securityModules);
104111

105112
List<String> jacksonModuleClassNameList = new ArrayList<>();
106113
jacksonModuleClassNameList.add(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.security.oauth2;
18+
19+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
20+
import com.fasterxml.jackson.annotation.JsonCreator;
21+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
24+
25+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
26+
@JsonAutoDetect(
27+
fieldVisibility = JsonAutoDetect.Visibility.ANY,
28+
getterVisibility = JsonAutoDetect.Visibility.NONE,
29+
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
30+
creatorVisibility = JsonAutoDetect.Visibility.NONE)
31+
@JsonIgnoreProperties(ignoreUnknown = true)
32+
abstract class AuthorizationGrantTypeMixin {
33+
34+
@JsonCreator
35+
public AuthorizationGrantTypeMixin(@JsonProperty("value") String value) {}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.security.oauth2;
18+
19+
import java.util.Collection;
20+
21+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
22+
import com.fasterxml.jackson.annotation.JsonCreator;
23+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
24+
import com.fasterxml.jackson.annotation.JsonProperty;
25+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
26+
import org.springframework.security.core.GrantedAuthority;
27+
import org.springframework.security.oauth2.core.OAuth2AccessToken;
28+
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
29+
30+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
31+
@JsonAutoDetect(
32+
fieldVisibility = JsonAutoDetect.Visibility.ANY,
33+
getterVisibility = JsonAutoDetect.Visibility.NONE,
34+
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
35+
creatorVisibility = JsonAutoDetect.Visibility.NONE)
36+
@JsonIgnoreProperties(ignoreUnknown = true)
37+
abstract class BearerTokenAuthenticationMixin {
38+
39+
@JsonCreator
40+
public BearerTokenAuthenticationMixin(
41+
@JsonProperty("principal") OAuth2AuthenticatedPrincipal principal,
42+
@JsonProperty("credentials") OAuth2AccessToken credentials,
43+
@JsonProperty("authorities") Collection<? extends GrantedAuthority> authorities) {}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.security.oauth2;
18+
19+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
20+
import com.fasterxml.jackson.annotation.JsonCreator;
21+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
24+
25+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
26+
@JsonAutoDetect(
27+
fieldVisibility = JsonAutoDetect.Visibility.ANY,
28+
getterVisibility = JsonAutoDetect.Visibility.NONE,
29+
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
30+
creatorVisibility = JsonAutoDetect.Visibility.NONE)
31+
@JsonIgnoreProperties(ignoreUnknown = true)
32+
abstract class ClientAuthenticationMethodMixin {
33+
34+
@JsonCreator
35+
public ClientAuthenticationMethodMixin(@JsonProperty("value") String value) {}
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.security.oauth2;
18+
19+
import java.util.Map;
20+
21+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
22+
import com.fasterxml.jackson.annotation.JsonCreator;
23+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
24+
import com.fasterxml.jackson.annotation.JsonProperty;
25+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
26+
27+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
28+
@JsonAutoDetect(
29+
fieldVisibility = JsonAutoDetect.Visibility.ANY,
30+
getterVisibility = JsonAutoDetect.Visibility.NONE,
31+
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
32+
creatorVisibility = JsonAutoDetect.Visibility.NONE)
33+
@JsonIgnoreProperties(ignoreUnknown = true)
34+
abstract class ClientSettingsMixin {
35+
36+
@JsonCreator
37+
public ClientSettingsMixin(@JsonProperty("settings") Map<String, Object> settings) {}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.security.oauth2;
18+
19+
import java.util.Collection;
20+
import java.util.Map;
21+
22+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
23+
import com.fasterxml.jackson.annotation.JsonCreator;
24+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
25+
import com.fasterxml.jackson.annotation.JsonProperty;
26+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
27+
import org.springframework.security.core.GrantedAuthority;
28+
29+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
30+
@JsonAutoDetect(
31+
fieldVisibility = JsonAutoDetect.Visibility.ANY,
32+
getterVisibility = JsonAutoDetect.Visibility.NONE,
33+
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
34+
creatorVisibility = JsonAutoDetect.Visibility.NONE)
35+
@JsonIgnoreProperties(ignoreUnknown = true)
36+
abstract class OAuth2AuthenticatedPrincipalMixin {
37+
38+
@JsonCreator
39+
public OAuth2AuthenticatedPrincipalMixin(
40+
@JsonProperty("name") String name,
41+
@JsonProperty("attributes") Map<String, Object> attributes,
42+
@JsonProperty("authorities") Collection<? extends GrantedAuthority> authorities) {}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.security.oauth2;
18+
19+
import java.util.Map;
20+
21+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
22+
import com.fasterxml.jackson.annotation.JsonCreator;
23+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
24+
import com.fasterxml.jackson.annotation.JsonProperty;
25+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
26+
import org.springframework.lang.Nullable;
27+
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
28+
29+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
30+
@JsonAutoDetect(
31+
fieldVisibility = JsonAutoDetect.Visibility.ANY,
32+
getterVisibility = JsonAutoDetect.Visibility.NONE,
33+
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
34+
creatorVisibility = JsonAutoDetect.Visibility.NONE)
35+
@JsonIgnoreProperties(ignoreUnknown = true)
36+
abstract class OAuth2ClientAuthenticationTokenMixin {
37+
38+
@JsonCreator
39+
public OAuth2ClientAuthenticationTokenMixin(
40+
@JsonProperty("clientId") String clientId,
41+
@JsonProperty("clientAuthenticationMethod") ClientAuthenticationMethod clientAuthenticationMethod,
42+
@JsonProperty("credentials") @Nullable Object credentials,
43+
@JsonProperty("additionalParameters") @Nullable Map<String, Object> additionalParameters) {}
44+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.security.oauth2;
18+
19+
import org.apache.dubbo.common.utils.ClassUtils;
20+
21+
import java.util.ArrayList;
22+
import java.util.Collections;
23+
24+
import com.fasterxml.jackson.databind.module.SimpleModule;
25+
26+
public class OAuth2SecurityModule extends SimpleModule {
27+
28+
public OAuth2SecurityModule() {
29+
super(OAuth2SecurityModule.class.getName());
30+
}
31+
32+
@Override
33+
public void setupModule(SetupContext context) {
34+
setMixInAnnotations(
35+
context,
36+
"org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal",
37+
"org.apache.dubbo.spring.security.oauth2.OAuth2AuthenticatedPrincipalMixin");
38+
setMixInAnnotations(
39+
context,
40+
"org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal",
41+
"org.apache.dubbo.spring.security.oauth2.OAuth2AuthenticatedPrincipalMixin");
42+
setMixInAnnotations(
43+
context,
44+
"org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication",
45+
"org.apache.dubbo.spring.security.oauth2.BearerTokenAuthenticationMixin");
46+
setMixInAnnotations(
47+
context,
48+
"org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken",
49+
"org.apache.dubbo.spring.security.oauth2.OAuth2ClientAuthenticationTokenMixin");
50+
setMixInAnnotations(
51+
context,
52+
"org.springframework.security.oauth2.core.ClientAuthenticationMethod",
53+
ClientAuthenticationMethodMixin.class);
54+
setMixInAnnotations(
55+
context,
56+
"org.springframework.security.oauth2.server.authorization.client.RegisteredClient",
57+
"org.apache.dubbo.spring.security.oauth2.RegisteredClientMixin");
58+
setMixInAnnotations(
59+
context,
60+
"org.springframework.security.oauth2.core.AuthorizationGrantType",
61+
AuthorizationGrantTypeMixin.class);
62+
setMixInAnnotations(
63+
context,
64+
"org.springframework.security.oauth2.server.authorization.settings.ClientSettings",
65+
ClientSettingsMixin.class);
66+
setMixInAnnotations(
67+
context,
68+
"org.springframework.security.oauth2.server.authorization.settings.TokenSettings",
69+
TokenSettingsMixin.class);
70+
context.setMixInAnnotations(
71+
Collections.unmodifiableCollection(new ArrayList<>()).getClass(), UnmodifiableCollectionMixin.class);
72+
}
73+
74+
private void setMixInAnnotations(SetupContext context, String oauth2ClassName, String mixinClassName) {
75+
Class<?> oauth2Class = loadClassIfPresent(oauth2ClassName);
76+
if (oauth2Class != null) {
77+
context.setMixInAnnotations(oauth2Class, loadClassIfPresent(mixinClassName));
78+
}
79+
}
80+
81+
private void setMixInAnnotations(SetupContext context, String oauth2ClassName, Class<?> mixinClass) {
82+
Class<?> oauth2Class = loadClassIfPresent(oauth2ClassName);
83+
if (oauth2Class != null) {
84+
context.setMixInAnnotations(oauth2Class, mixinClass);
85+
}
86+
}
87+
88+
private Class<?> loadClassIfPresent(String oauth2ClassName) {
89+
try {
90+
return ClassUtils.forName(oauth2ClassName, OAuth2SecurityModule.class.getClassLoader());
91+
92+
} catch (Throwable ignored) {
93+
}
94+
return null;
95+
}
96+
}

0 commit comments

Comments
 (0)