Skip to content

Commit b33f99a

Browse files
ci/cd : add samples to the client folder
1 parent c2874e2 commit b33f99a

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

client/src/main/java/com/patternknife/securityhelper/oauth2/client/domain/common/api/BaseApi.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.patternknife.securityhelper.oauth2.client.domain.common.api;
22

33
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.security.access.prepost.PreAuthorize;
45
import org.springframework.web.bind.annotation.GetMapping;
56
import 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 () {

0 commit comments

Comments
 (0)