Skip to content

Commit 041ab5e

Browse files
committed
feat(server): anonymous read access
Enable anonymous read access for GET requests to /w3c/* and /oa/* endpoints even when auth.enabled=true.
1 parent 713ad41 commit 041ab5e

3 files changed

Lines changed: 36 additions & 11 deletions

File tree

elucidate-server/src/main/java/com/digirati/elucidate/infrastructure/config/AuthConfig.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.digirati.elucidate.repository.security.GroupRepository;
1212
import com.digirati.elucidate.repository.security.UserRepository;
1313
import org.apache.commons.lang3.StringUtils;
14+
import org.springframework.http.HttpMethod;
1415
import org.springframework.beans.factory.annotation.Autowired;
1516
import org.springframework.beans.factory.annotation.Qualifier;
1617
import org.springframework.beans.factory.annotation.Value;
@@ -19,7 +20,6 @@
1920
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
2021
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2122
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
22-
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
2323
import org.springframework.security.config.http.SessionCreationPolicy;
2424
import org.springframework.security.jwt.crypto.sign.MacSigner;
2525
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
@@ -69,11 +69,17 @@ public class AuthConfig implements ResourceServerConfigurer {
6969
private String uidProperties;
7070

7171
/**
72-
* The public key used to verify a tokens signature.
72+
* Is auth enabled?
7373
*/
7474
@Value("${auth.enabled:false}")
7575
private boolean authEnabled;
7676

77+
/**
78+
* Allow anonymous access to /w3c/* and /oa/* endpoints even when auth is enabled?
79+
*/
80+
@Value("${auth.anonReadAccess:false}")
81+
private boolean anonReadAccess;
82+
7783
/**
7884
* The URL scheme that will be used in the OAuth2 resource id.
7985
*/
@@ -119,16 +125,30 @@ public void configure(ResourceServerSecurityConfigurer resources) throws Excepti
119125

120126
@Override
121127
public void configure(HttpSecurity http) throws Exception {
122-
ExpressionUrlAuthorizationConfigurer.AuthorizedUrl authorizationConfigurer = http
123-
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
124-
.and()
125-
.authorizeRequests()
126-
.anyRequest();
127-
128-
if (authEnabled) {
129-
authorizationConfigurer.authenticated();
128+
HttpSecurity authorizationConfigurer = http
129+
.sessionManagement()
130+
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
131+
.and();
132+
133+
if (authEnabled && anonReadAccess) {
134+
authorizationConfigurer
135+
.authorizeRequests()
136+
.antMatchers(HttpMethod.GET, "/w3c/**", "/oa/**")
137+
.permitAll()
138+
.and()
139+
.authorizeRequests()
140+
.anyRequest()
141+
.authenticated();
142+
} else if (authEnabled) {
143+
authorizationConfigurer
144+
.authorizeRequests()
145+
.anyRequest()
146+
.authenticated();
130147
} else {
131-
authorizationConfigurer.permitAll();
148+
authorizationConfigurer
149+
.authorizeRequests()
150+
.anyRequest()
151+
.permitAll();
132152
}
133153
}
134154

elucidate-server/src/main/java/com/digirati/elucidate/infrastructure/security/impl/JwtUserSecurityDetailsContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.security.core.GrantedAuthority;
1111
import org.springframework.security.core.authority.SimpleGrantedAuthority;
1212
import org.springframework.security.core.context.SecurityContextHolder;
13+
import org.springframework.security.authentication.AnonymousAuthenticationToken;
1314

1415
import java.util.Collection;
1516

@@ -20,6 +21,9 @@ public class JwtUserSecurityDetailsContext implements UserSecurityDetailsContext
2021
@Override
2122
public boolean isAuthorized(Permission operation, AbstractAnnotation annotation) {
2223
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
24+
if (auth instanceof AnonymousAuthenticationToken) {
25+
return true;
26+
}
2327
UserSecurityDetails details = (UserSecurityDetails) auth.getPrincipal();
2428
Collection<? extends GrantedAuthority> roles = auth.getAuthorities();
2529

elucidate-server/src/main/resources/eludicate-server.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ auth.enabled=false
145145
auth.token.verifierType=secret
146146
auth.token.verifierKey=
147147
auth.token.uidProperties=sub,user_name
148+
auth.anonReadAccess=false
148149

149150
# Generator to use when generating Security Group IDs
150151
annotation.group.id.generator=com.digirati.elucidate.infrastructure.generator.impl.UUIDIDGeneratorImpl

0 commit comments

Comments
 (0)