SpringBoot: customUserDetails

- remove unnecessary Custom authenticatio provider
- cosmetic changes to ApiUserDetailsService
- rename ShortHashService to ShortHashManager
This commit is contained in:
djmil 2023-08-27 16:56:50 +02:00
parent 23560c8326
commit 43c4252df1
5 changed files with 25 additions and 105 deletions

View File

@ -33,8 +33,11 @@ public class ApiController {
*/ */
@GetMapping("/api/activegames") @GetMapping("/api/activegames")
public ResponseEntity<String> dashboard(@AuthenticationPrincipal ApiUserDetails user) { public ResponseEntity<String> dashboard(@AuthenticationPrincipal ApiUserDetails user) {
System.out.println("List of active games for "
+ "user: " + user.getUsername()
+ " with shortIdentityHash: " + user.getShortHash());
return ResponseEntity.ok("{ \"ActiveGames\" : [\"game\", \"GAME\", \""+user.getShortHash()+ "\" ] }" ); return ResponseEntity.ok("{ \"ActiveGames\" : [\"id_game1\", \"id_game2\"] }" );
} }
} }

View File

@ -11,33 +11,31 @@ import org.springframework.stereotype.Service;
public class ApiUserDetailsService implements UserDetailsService { public class ApiUserDetailsService implements UserDetailsService {
private final PasswordEncoder encoder; private final PasswordEncoder encoder;
private final ApiUserShortHashService apiUserShortHash; private final ShortHashManager shortHashManager;
public ApiUserDetailsService(PasswordEncoder encoder, ApiUserShortHashService apiUserShortHash) { public ApiUserDetailsService(
PasswordEncoder encoder,
ShortHashManager shortHashManager) {
this.encoder = encoder; this.encoder = encoder;
this.apiUserShortHash = apiUserShortHash; this.shortHashManager = shortHashManager;
} }
@Override @Override
public ApiUserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public ApiUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// Load user from database String shortHash = shortHashManager.getShortHashBy(username);
// User user = userRepository.findByUsername(username); if (shortHash == null) {
throw new UsernameNotFoundException("ShortHash for user '"
// if (user == null) { +username+ "' not found");
// throw new UsernameNotFoundException("User not found"); }
// }
System.out.println("Load user "+username); System.out.println("Load user "+username);
User.UserBuilder users = User.builder(); User.UserBuilder userBuilder = User.builder();
UserDetails user = userBuilder
UserDetails alice = users
.username(username) .username(username)
.password(encoder.encode("qaz123")) .password(encoder.encode("qaz123"))
.build(); .build();
ApiUserDetails apiUser = new ApiUserDetails(alice, apiUserShortHash.getShortHashBy(username)); return new ApiUserDetails(user, shortHash);
return apiUser;
} }
} }

View File

@ -1,45 +0,0 @@
package djmil.cordacheckers;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@Service
public class CustomAuthenticationProvider implements AuthenticationProvider {
private final PasswordEncoder encoder;
private final ApiUserDetailsService userDetailsService;
public CustomAuthenticationProvider(PasswordEncoder encoder, ApiUserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
this.encoder = encoder;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = authentication.getCredentials().toString();
ApiUserDetails user = userDetailsService.loadUserByUsername(username);
return checkPassword(user, password);
}
@Override
public boolean supports(Class<?> aClass) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(aClass);
}
private Authentication checkPassword(ApiUserDetails user, String rawPassword) {
if (encoder.matches(rawPassword, user.getPassword())) {
return new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
} else {
throw new BadCredentialsException("Bad credentials");
}
}
}

View File

@ -2,15 +2,9 @@ package djmil.cordacheckers;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import static org.springframework.security.config.Customizer.withDefaults;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@ -21,34 +15,4 @@ public class SecurityConfig {
return PasswordEncoderFactories.createDelegatingPasswordEncoder(); return PasswordEncoderFactories.createDelegatingPasswordEncoder();
} }
public void configure(AuthenticationManagerBuilder auth, CustomAuthenticationProvider authenticationProvider) throws Exception {
auth.authenticationProvider(authenticationProvider);
}
// @Bean
// SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
// http
// .authorizeRequests(authorizeRequests ->
// authorizeRequests.anyRequest().authenticated());
// .formLogin(withDefaults());
// return http.build();
// }
// @Bean
// UserDetailsService hardcodedCordaUsers(PasswordEncoder passwordEncoder) {
// User.UserBuilder users = User.builder();
// UserDetails alice = users
// .username("alice")
// .password(passwordEncoder.encode("qaz123"))
// .build();
// UserDetails bob = users
// .username("bob")
// .password(passwordEncoder.encode("qaz123"))
// .build();
// return new InMemoryUserDetailsManager(alice, bob);
// }
} }

View File

@ -13,16 +13,16 @@ import djmil.cordacheckers.cordaclient.CordaClient;
import djmil.cordacheckers.cordaclient.pojo.virtualNodes; import djmil.cordacheckers.cordaclient.pojo.virtualNodes;
@Service @Service
public class ApiUserShortHashService { public class ShortHashManager {
static final Locale locale = Locale.getDefault(); static final Locale locale = Locale.getDefault();
Map<String, String> apiUserShortHashMap; Map<String, String> cnName2shortHash;
ApiUserShortHashService(CordaClient client) { ShortHashManager(CordaClient client) {
this.apiUserShortHashMap = setApiUserShortHashMap(client); this.cnName2shortHash = setCnName2shortHash(client);
} }
private static Map<String, String> setApiUserShortHashMap(CordaClient client) { private static Map<String, String> setCnName2shortHash(CordaClient client) {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
List<virtualNodes> vNodesList = client.getVirtualnode(); List<virtualNodes> vNodesList = client.getVirtualnode();
@ -37,7 +37,7 @@ public class ApiUserShortHashService {
} }
} catch (InvalidNameException e) { } catch (InvalidNameException e) {
// TODO: logs // TODO: logs
System.out.println("Unable to get ShorHash map for vNode: "+e.getExplanation()); System.out.println("Unable to get ShorHash map for Corda virtual nodes: "+e.getExplanation());
e.printStackTrace(); e.printStackTrace();
} }
@ -46,6 +46,6 @@ public class ApiUserShortHashService {
} }
String getShortHashBy(String apiUserName) { String getShortHashBy(String apiUserName) {
return this.apiUserShortHashMap.get(apiUserName.toLowerCase(locale)); return this.cnName2shortHash.get(apiUserName.toLowerCase(locale));
} }
} }