File tree Expand file tree Collapse file tree 2 files changed +33
-2
lines changed
client/src/main/java/com/patternknife/securityhelper/oauth2/client
config/securityimpl/serivce/permission Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change
1
+ package com .patternknife .securityhelper .oauth2 .client .config .securityimpl .serivce .permission ;
2
+
3
+ import io .github .patternknife .securityhelper .oauth2 .api .config .security .response .error .exception .KnifeOauth2AuthenticationException ;
4
+ import org .springframework .security .core .Authentication ;
5
+ import org .springframework .security .core .context .SecurityContextHolder ;
6
+ import org .springframework .stereotype .Service ;
7
+
8
+ @ Service
9
+ public class CustomAuthorityService {
10
+
11
+ public boolean hasAnyUserRole () {
12
+ Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
13
+ if (authentication == null ){
14
+ throw new KnifeOauth2AuthenticationException ();
15
+ }
16
+ if (authentication .getAuthorities () == null ) {
17
+ return false ;
18
+ }
19
+ return authentication .getAuthorities ().stream ()
20
+ .anyMatch (authority -> authority .getAuthority ().endsWith ("_USER" ));
21
+ }
22
+
23
+ }
Original file line number Diff line number Diff line change 1
1
package com .patternknife .securityhelper .oauth2 .client .domain .common .api ;
2
2
3
3
import org .springframework .beans .factory .annotation .Value ;
4
+ import org .springframework .security .access .prepost .PreAuthorize ;
4
5
import org .springframework .web .bind .annotation .GetMapping ;
5
6
import org .springframework .web .bind .annotation .RestController ;
6
7
@@ -10,12 +11,19 @@ public class BaseApi {
10
11
@ Value ("${spring.profiles.active}" )
11
12
private String activeProfile ;
12
13
13
-
14
+ @ PreAuthorize ( "isAuthenticated()" )
14
15
@ GetMapping ("/systemProfile" )
15
- public String getProfile () {
16
+ public String checkAuthenticated () {
16
17
return activeProfile ;
17
18
}
18
19
20
+ @ PreAuthorize ("@customAuthorityService.hasAnyUserRole()" )
21
+ @ GetMapping ("/systemProfile2" )
22
+ public String checkPermissions () {
23
+ return activeProfile ;
24
+ }
25
+
26
+
19
27
20
28
@ GetMapping ("/" )
21
29
public String home () {
You can’t perform that action at this time.
0 commit comments