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 11package com .patternknife .securityhelper .oauth2 .client .domain .common .api ;
22
33import org .springframework .beans .factory .annotation .Value ;
4+ import org .springframework .security .access .prepost .PreAuthorize ;
45import org .springframework .web .bind .annotation .GetMapping ;
56import org .springframework .web .bind .annotation .RestController ;
67
@@ -10,12 +11,19 @@ public class BaseApi {
1011 @ Value ("${spring.profiles.active}" )
1112 private String activeProfile ;
1213
13-
14+ @ PreAuthorize ( "isAuthenticated()" )
1415 @ GetMapping ("/systemProfile" )
15- public String getProfile () {
16+ public String checkAuthenticated () {
1617 return activeProfile ;
1718 }
1819
20+ @ PreAuthorize ("@customAuthorityService.hasAnyUserRole()" )
21+ @ GetMapping ("/systemProfile2" )
22+ public String checkPermissions () {
23+ return activeProfile ;
24+ }
25+
26+
1927
2028 @ GetMapping ("/" )
2129 public String home () {
You can’t perform that action at this time.
0 commit comments