forked from spring-attic/spring-security-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JsonParser utility interface so that JWT can choose one
At runtime Jackson (1) might not be available on the classpath and up to now the JwtAccessTokenConverter relied on it coming from spring-security-jwt transitively. The latest version of that library no longer requires an external JSON parser, so we need a way to choose a converter/parser at runtime. Fixes spring-atticgh-391
- Loading branch information
Showing
7 changed files
with
179 additions
and
7 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...th2/src/main/java/org/springframework/security/oauth2/common/util/Jackson2JsonParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2013-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.oauth2.common.util; | ||
|
||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
|
||
|
||
/** | ||
* @author Dave Syer | ||
* | ||
*/ | ||
public class Jackson2JsonParser implements JsonParser { | ||
|
||
private ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Map<String, Object> parseMap(String json) { | ||
try { | ||
return mapper.readValue(json, Map.class); | ||
} | ||
catch (Exception e) { | ||
throw new IllegalArgumentException("Cannot parse json", e); | ||
} | ||
} | ||
|
||
@Override | ||
public String formatMap(Map<String, ?> map) { | ||
try { | ||
return mapper.writeValueAsString(map); | ||
} | ||
catch (Exception e) { | ||
throw new IllegalArgumentException("Cannot format json", e); | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...uth2/src/main/java/org/springframework/security/oauth2/common/util/JacksonJsonParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2013-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.oauth2.common.util; | ||
|
||
import java.util.Map; | ||
|
||
import org.codehaus.jackson.map.ObjectMapper; | ||
|
||
/** | ||
* @author Dave Syer | ||
* | ||
*/ | ||
public class JacksonJsonParser implements JsonParser { | ||
|
||
private ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Map<String, Object> parseMap(String json) { | ||
try { | ||
return mapper.readValue(json, Map.class); | ||
} | ||
catch (Exception e) { | ||
throw new IllegalArgumentException("Cannot parse json", e); | ||
} | ||
} | ||
|
||
@Override | ||
public String formatMap(Map<String, ?> map) { | ||
try { | ||
return mapper.writeValueAsString(map); | ||
} | ||
catch (Exception e) { | ||
throw new IllegalArgumentException("Cannot format json", e); | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...rity-oauth2/src/main/java/org/springframework/security/oauth2/common/util/JsonParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2013-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.oauth2.common.util; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author Dave Syer | ||
* | ||
*/ | ||
public interface JsonParser { | ||
|
||
/** | ||
* Parse the specified JSON string into a Map. | ||
* @param json the JSON to parse | ||
* @return the parsed JSON as a map | ||
*/ | ||
Map<String, Object> parseMap(String json); | ||
|
||
/** | ||
* Convert the Map to JSON | ||
* @param map a map to format | ||
* @return a JSON representation of the map | ||
*/ | ||
String formatMap(Map<String, ?> map); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...uth2/src/main/java/org/springframework/security/oauth2/common/util/JsonParserFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2013-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.oauth2.common.util; | ||
|
||
import org.springframework.util.ClassUtils; | ||
|
||
/** | ||
* @author Dave Syer | ||
* | ||
*/ | ||
public class JsonParserFactory { | ||
|
||
public static JsonParser create() { | ||
if (ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", null)) { | ||
return new Jackson2JsonParser(); | ||
} | ||
if (ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", null)) { | ||
return new JacksonJsonParser(); | ||
} | ||
throw new IllegalStateException("No Jackson parser found. Please add Jackson to your classpath."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters