40
40
* @since 5.2
41
41
*/
42
42
public class JwtGrantedAuthoritiesConverterTests {
43
- private JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter ();
44
43
45
44
@ Test
46
45
public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities () {
47
46
Jwt jwt = this .jwt (Collections .singletonMap ("scope" , "message:read message:write" ));
48
47
49
- Collection <GrantedAuthority > authorities = this .jwtGrantedAuthoritiesConverter .convert (jwt );
48
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter ();
49
+ Collection <GrantedAuthority > authorities = jwtGrantedAuthoritiesConverter .convert (jwt );
50
50
51
51
assertThat (authorities ).containsExactly (
52
52
new SimpleGrantedAuthority ("SCOPE_message:read" ),
53
53
new SimpleGrantedAuthority ("SCOPE_message:write" ));
54
54
}
55
55
56
+ @ Test
57
+ public void convertWithCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities () {
58
+ Jwt jwt = this .jwt (Collections .singletonMap ("scope" , "message:read message:write" ));
59
+
60
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter ();
61
+ jwtGrantedAuthoritiesConverter .setAuthorityPrefix ("ROLE_" );
62
+ Collection <GrantedAuthority > authorities = jwtGrantedAuthoritiesConverter .convert (jwt );
63
+
64
+ assertThat (authorities ).containsExactly (
65
+ new SimpleGrantedAuthority ("ROLE_message:read" ),
66
+ new SimpleGrantedAuthority ("ROLE_message:write" ));
67
+ }
68
+
56
69
@ Test
57
70
public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities () {
58
71
Jwt jwt = this .jwt (Collections .singletonMap ("scope" , "" ));
59
72
60
- Collection <GrantedAuthority > authorities = this .jwtGrantedAuthoritiesConverter .convert (jwt );
73
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter ();
74
+ Collection <GrantedAuthority > authorities = jwtGrantedAuthoritiesConverter .convert (jwt );
61
75
62
- assertThat (authorities ).containsExactly ();
76
+ assertThat (authorities ).isEmpty ();
63
77
}
64
78
65
79
@ Test
66
80
public void convertWhenTokenHasScpAttributeThenTranslatedToAuthorities () {
67
81
Jwt jwt = this .jwt (Collections .singletonMap ("scp" , Arrays .asList ("message:read" , "message:write" )));
68
82
69
- Collection <GrantedAuthority > authorities = this .jwtGrantedAuthoritiesConverter .convert (jwt );
83
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter ();
84
+ Collection <GrantedAuthority > authorities = jwtGrantedAuthoritiesConverter .convert (jwt );
70
85
71
86
assertThat (authorities ).containsExactly (
72
87
new SimpleGrantedAuthority ("SCOPE_message:read" ),
73
88
new SimpleGrantedAuthority ("SCOPE_message:write" ));
74
89
}
75
90
91
+ @ Test
92
+ public void convertWithCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities () {
93
+ Jwt jwt = this .jwt (Collections .singletonMap ("scp" , Arrays .asList ("message:read" , "message:write" )));
94
+
95
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter ();
96
+ jwtGrantedAuthoritiesConverter .setAuthorityPrefix ("ROLE_" );
97
+ Collection <GrantedAuthority > authorities = jwtGrantedAuthoritiesConverter .convert (jwt );
98
+
99
+ assertThat (authorities ).containsExactly (
100
+ new SimpleGrantedAuthority ("ROLE_message:read" ),
101
+ new SimpleGrantedAuthority ("ROLE_message:write" ));
102
+ }
103
+
76
104
@ Test
77
105
public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities () {
78
- Jwt jwt = this .jwt (Maps .newHashMap ("scp" , Arrays . asList ()));
106
+ Jwt jwt = this .jwt (Maps .newHashMap ("scp" , Collections . emptyList ()));
79
107
80
- Collection <GrantedAuthority > authorities = this .jwtGrantedAuthoritiesConverter .convert (jwt );
108
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter ();
109
+ Collection <GrantedAuthority > authorities = jwtGrantedAuthoritiesConverter .convert (jwt );
81
110
82
- assertThat (authorities ).containsExactly ();
111
+ assertThat (authorities ).isEmpty ();
83
112
}
84
113
85
114
@ Test
@@ -89,7 +118,8 @@ public void convertWhenTokenHasBothScopeAndScpThenScopeAttributeIsTranslatedToAu
89
118
claims .put ("scope" , "missive:read missive:write" );
90
119
Jwt jwt = this .jwt (claims );
91
120
92
- Collection <GrantedAuthority > authorities = this .jwtGrantedAuthoritiesConverter .convert (jwt );
121
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter ();
122
+ Collection <GrantedAuthority > authorities = jwtGrantedAuthoritiesConverter .convert (jwt );
93
123
94
124
assertThat (authorities ).containsExactly (
95
125
new SimpleGrantedAuthority ("SCOPE_missive:read" ),
@@ -103,9 +133,10 @@ public void convertWhenTokenHasEmptyScopeAndNonEmptyScpThenScopeAttributeIsTrans
103
133
claims .put ("scope" , "" );
104
134
Jwt jwt = this .jwt (claims );
105
135
106
- Collection <GrantedAuthority > authorities = this .jwtGrantedAuthoritiesConverter .convert (jwt );
136
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter ();
137
+ Collection <GrantedAuthority > authorities = jwtGrantedAuthoritiesConverter .convert (jwt );
107
138
108
- assertThat (authorities ).containsExactly ();
139
+ assertThat (authorities ).isEmpty ();
109
140
}
110
141
111
142
private Jwt jwt (Map <String , Object > claims ) {
0 commit comments