Skip to content

Commit

Permalink
Adding SpringUserAdapter and making it implement UserDetails. User cl…
Browse files Browse the repository at this point in the history
…ass is now free of Spring, Adapter takes care about that.
  • Loading branch information
jacekbilski committed Mar 29, 2020
1 parent 9fdbd23 commit 5230203
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 45 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/com/webcalc/billing/BillingController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.webcalc.billing;

import com.webcalc.user.SpringUserAdapter;
import com.webcalc.user.User;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -19,6 +20,7 @@ public BillingController(Billing billing) {
@GetMapping("/balance")
public String getBalance(HttpServletRequest request) {
Authentication auth = (Authentication) request.getUserPrincipal();
return billing.getBalance(((User) auth.getPrincipal()).id).toPlainString();
User user = ((SpringUserAdapter) auth.getPrincipal()).getUser();
return billing.getBalance(user.id).toPlainString();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.webcalc.calculator;

import com.webcalc.user.SpringUserAdapter;
import com.webcalc.user.User;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -26,7 +27,8 @@ public String calculate(@RequestBody String body, HttpSession session, HttpServl
if (session.getAttribute(MAX_FRACTION_DIGITS) == null)
session.setAttribute(MAX_FRACTION_DIGITS, Calculator.DEFAULT_MAX_FRACTION_DIGITS);
Authentication auth = (Authentication) request.getUserPrincipal();
return calculator.eval(((User) auth.getPrincipal()).id, body, (Integer) session.getAttribute(MAX_FRACTION_DIGITS));
User user = ((SpringUserAdapter) auth.getPrincipal()).getUser();
return calculator.eval(user.id, body, (Integer) session.getAttribute(MAX_FRACTION_DIGITS));
}

@PutMapping("/maxFractionDigits")
Expand Down
56 changes: 56 additions & 0 deletions app/src/main/java/com/webcalc/user/SpringUserAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.webcalc.user;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

import static java.util.Collections.emptyList;

public class SpringUserAdapter implements UserDetails {

private final User user;

public SpringUserAdapter(User user) {
this.user = user;
}

public User getUser() {
return user;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return emptyList();
}

@Override
public String getPassword() {
return user.getPassword();
}

@Override
public String getUsername() {
return user.getUsername();
}

@Override
public boolean isAccountNonExpired() {
return true;
}

@Override
public boolean isAccountNonLocked() {
return true;
}

@Override
public boolean isCredentialsNonExpired() {
return true;
}

@Override
public boolean isEnabled() {
return true;
}
}
50 changes: 8 additions & 42 deletions app/src/main/java/com/webcalc/user/User.java
Original file line number Diff line number Diff line change
@@ -1,58 +1,24 @@
package com.webcalc.user;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.UUID;

public class User implements UserDetails {
public class User {

public final UUID id;

private final UserDetails springSecUser;
private final String username;
private final String password;

public User(UUID id, String username, String password) {
this.id = id;
springSecUser = org.springframework.security.core.userdetails.User.builder()
.username(username)
.password(password)
.roles()
.build();
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return springSecUser.getAuthorities();
}

@Override
public String getPassword() {
return springSecUser.getPassword();
this.username = username;
this.password = password;
}

@Override
public String getUsername() {
return springSecUser.getUsername();
return username;
}

@Override
public boolean isAccountNonExpired() {
return springSecUser.isAccountNonExpired();
}

@Override
public boolean isAccountNonLocked() {
return springSecUser.isAccountNonLocked();
}

@Override
public boolean isCredentialsNonExpired() {
return springSecUser.isCredentialsNonExpired();
}

@Override
public boolean isEnabled() {
return springSecUser.isEnabled();
public String getPassword() {
return password;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class WebCalcUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
if (users.containsKey(username))
return users.get(username);
return new SpringUserAdapter(users.get(username));
throw new UsernameNotFoundException("User unknown");
}
}

0 comments on commit 5230203

Please sign in to comment.