Skip to content

Commit fe68836

Browse files
committed
updating the spring security used by save and restore
1 parent 46a105e commit fe68836

3 files changed

Lines changed: 20 additions & 28 deletions

File tree

services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/config/WebSecurityConfig.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
1212
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
1313
import org.springframework.security.authentication.AuthenticationManager;
14-
import org.springframework.security.config.annotation.ObjectPostProcessor;
14+
import org.springframework.security.authentication.ProviderManager;
15+
import org.springframework.security.config.Customizer;
16+
import org.springframework.security.config.ObjectPostProcessor;
1517
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
1618
import org.springframework.security.config.annotation.authentication.configurers.ldap.LdapAuthenticationProviderConfigurer;
17-
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
19+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
1820
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1921
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
2022
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
@@ -35,7 +37,7 @@
3537
*/
3638
@Configuration
3739
@EnableWebSecurity
38-
@EnableGlobalMethodSecurity(prePostEnabled = true)
40+
@EnableMethodSecurity(prePostEnabled = true)
3941
@SuppressWarnings("unused")
4042
public class WebSecurityConfig {
4143

@@ -188,8 +190,8 @@ public String authenticationImplementation(){
188190
public WebSecurityCustomizer ignoringCustomizer() {
189191
return web -> {
190192
// The below lists exceptions for authentication.
191-
web.ignoring().antMatchers(HttpMethod.GET, "/**");
192-
web.ignoring().antMatchers(HttpMethod.POST, "/**/login*");
193+
web.ignoring().requestMatchers(HttpMethod.GET, "/**");
194+
web.ignoring().requestMatchers(HttpMethod.POST, "/**/login*");
193195
};
194196
}
195197

@@ -201,9 +203,9 @@ public WebSecurityCustomizer ignoringCustomizer() {
201203
*/
202204
@Bean
203205
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
204-
http.csrf().disable();
205-
http.authorizeRequests().anyRequest().authenticated();
206-
http.httpBasic();
206+
http.csrf(csrf -> csrf.disable());
207+
http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated());
208+
http.httpBasic(Customizer.withDefaults());
207209
return http.build();
208210
}
209211

@@ -284,12 +286,7 @@ public AuthenticationManager authenticationProvider() throws Exception {
284286
SimpleAuthorityMapper simpleAuthorityMapper = new SimpleAuthorityMapper();
285287
simpleAuthorityMapper.setConvertToUpperCase(true);
286288
adProvider.setAuthoritiesMapper(simpleAuthorityMapper);
287-
return new AuthenticationManagerBuilder(new ObjectPostProcessor<>() {
288-
@Override
289-
public <O> O postProcess(O object) {
290-
return object;
291-
}
292-
}).authenticationProvider(adProvider).build();
289+
return new ProviderManager(adProvider);
293290
}
294291

295292
/**
@@ -346,9 +343,7 @@ public ObjectMapper objectMapper() {
346343
*/
347344
@Bean
348345
public RoleHierarchy roleHierarchy() {
349-
RoleHierarchyImpl hierarchy = new RoleHierarchyImpl();
350-
hierarchy.setHierarchy("ROLE_" + roleAdmin.toUpperCase() + " > ROLE_" + roleUser.toUpperCase());
351-
return hierarchy;
346+
return RoleHierarchyImpl.fromHierarchy("ROLE_" + roleAdmin.toUpperCase() + " > ROLE_" + roleUser.toUpperCase());
352347
}
353348

354349
/**

services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/AuthenticationController.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,10 @@ public ResponseEntity<UserData> login(@RequestBody LoginCredentials loginCredent
6262
authentication = authenticationManager.authenticate(authentication);
6363
} catch (AuthenticationException e) {
6464
Logger.getLogger(AuthenticationController.class.getName()).log(Level.WARNING, "Unable to authenticate user " + loginCredentials.username(), e);
65-
return new ResponseEntity<>(
66-
null,
67-
HttpStatus.UNAUTHORIZED);
65+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
6866
}
6967
List<String> roles = authentication.getAuthorities().stream()
7068
.map(GrantedAuthority::getAuthority).collect(Collectors.toList());
71-
return new ResponseEntity<>(
72-
new UserData(loginCredentials.username(), roles),
73-
HttpStatus.OK);
69+
return ResponseEntity.ok(new UserData(loginCredentials.username(), roles));
7470
}
7571
}

services/save-and-restore/src/test/java/org/phoebus/service/saveandrestore/web/config/ControllersTestConfig.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
import org.springframework.messaging.simp.SimpMessagingTemplate;
3737
import org.springframework.messaging.simp.user.SimpUserRegistry;
3838
import org.springframework.mock.web.MockServletContext;
39-
import org.springframework.util.Base64Utils;
4039
import org.springframework.web.socket.WebSocketSession;
4140

42-
import javax.servlet.ServletContext;
41+
import jakarta.servlet.ServletContext;
42+
import java.nio.charset.StandardCharsets;
43+
import java.util.Base64;
4344
import java.util.concurrent.ExecutorService;
4445
import java.util.concurrent.Executors;
4546

@@ -111,17 +112,17 @@ public SearchUtil searchUtil() {
111112

112113
@Bean("userAuthorization")
113114
public String userAuthorization() {
114-
return "Basic " + Base64Utils.encodeToString((demoUser + ":" + demoUserPassword).getBytes());
115+
return "Basic " + Base64.getEncoder().encodeToString((demoUser + ":" + demoUserPassword).getBytes(StandardCharsets.UTF_8));
115116
}
116117

117118
@Bean("adminAuthorization")
118119
public String adminAuthorization() {
119-
return "Basic " + Base64Utils.encodeToString((demoAdmin + ":" + demoAdminPassword).getBytes());
120+
return "Basic " + Base64.getEncoder().encodeToString((demoAdmin + ":" + demoAdminPassword).getBytes(StandardCharsets.UTF_8));
120121
}
121122

122123
@Bean("readOnlyAuthorization")
123124
public String readOnlyAuthorization() {
124-
return "Basic " + Base64Utils.encodeToString((demoReadOnly + ":" + demoReadOnlyPassword).getBytes());
125+
return "Basic " + Base64.getEncoder().encodeToString((demoReadOnly + ":" + demoReadOnlyPassword).getBytes(StandardCharsets.UTF_8));
125126
}
126127

127128
@Bean

0 commit comments

Comments
 (0)