SpringBoot: customUserDetails
- remove unnecessary Custom authenticatio provider - cosmetic changes to ApiUserDetailsService - rename ShortHashService to ShortHashManager
This commit is contained in:
parent
23560c8326
commit
43c4252df1
@ -33,8 +33,11 @@ public class ApiController {
|
||||
*/
|
||||
@GetMapping("/api/activegames")
|
||||
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\"] }" );
|
||||
}
|
||||
|
||||
}
|
@ -11,33 +11,31 @@ import org.springframework.stereotype.Service;
|
||||
public class ApiUserDetailsService implements UserDetailsService {
|
||||
|
||||
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.apiUserShortHash = apiUserShortHash;
|
||||
this.shortHashManager = shortHashManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
// Load user from database
|
||||
// User user = userRepository.findByUsername(username);
|
||||
|
||||
// if (user == null) {
|
||||
// throw new UsernameNotFoundException("User not found");
|
||||
// }
|
||||
String shortHash = shortHashManager.getShortHashBy(username);
|
||||
if (shortHash == null) {
|
||||
throw new UsernameNotFoundException("ShortHash for user '"
|
||||
+username+ "' not found");
|
||||
}
|
||||
|
||||
System.out.println("Load user "+username);
|
||||
|
||||
User.UserBuilder users = User.builder();
|
||||
|
||||
UserDetails alice = users
|
||||
User.UserBuilder userBuilder = User.builder();
|
||||
UserDetails user = userBuilder
|
||||
.username(username)
|
||||
.password(encoder.encode("qaz123"))
|
||||
.build();
|
||||
|
||||
ApiUserDetails apiUser = new ApiUserDetails(alice, apiUserShortHash.getShortHashBy(username));
|
||||
|
||||
return apiUser;
|
||||
return new ApiUserDetails(user, shortHash);
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -2,15 +2,9 @@ package djmil.cordacheckers;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
import static org.springframework.security.config.Customizer.withDefaults;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@ -21,34 +15,4 @@ public class SecurityConfig {
|
||||
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);
|
||||
// }
|
||||
|
||||
}
|
||||
|
@ -13,16 +13,16 @@ import djmil.cordacheckers.cordaclient.CordaClient;
|
||||
import djmil.cordacheckers.cordaclient.pojo.virtualNodes;
|
||||
|
||||
@Service
|
||||
public class ApiUserShortHashService {
|
||||
public class ShortHashManager {
|
||||
static final Locale locale = Locale.getDefault();
|
||||
|
||||
Map<String, String> apiUserShortHashMap;
|
||||
Map<String, String> cnName2shortHash;
|
||||
|
||||
ApiUserShortHashService(CordaClient client) {
|
||||
this.apiUserShortHashMap = setApiUserShortHashMap(client);
|
||||
ShortHashManager(CordaClient 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<>();
|
||||
|
||||
List<virtualNodes> vNodesList = client.getVirtualnode();
|
||||
@ -37,7 +37,7 @@ public class ApiUserShortHashService {
|
||||
}
|
||||
} catch (InvalidNameException e) {
|
||||
// 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();
|
||||
}
|
||||
|
||||
@ -46,6 +46,6 @@ public class ApiUserShortHashService {
|
||||
}
|
||||
|
||||
String getShortHashBy(String apiUserName) {
|
||||
return this.apiUserShortHashMap.get(apiUserName.toLowerCase(locale));
|
||||
return this.cnName2shortHash.get(apiUserName.toLowerCase(locale));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user