-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Keesun Baik (a.k.a, Whiteship)
committed
Nov 10, 2018
1 parent
29e9c34
commit 3e50caf
Showing
7 changed files
with
86 additions
and
8 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
ksug201811restapi/src/main/java/me/whiteship/ksug201811restapi/accounts/AccountAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package me.whiteship.ksug201811restapi.accounts; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.User; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
|
||
public class AccountAdapter extends User { | ||
|
||
private Account account; | ||
|
||
public AccountAdapter(Account account) { | ||
super(account.getUsername(), account.getPassword(), authorities(account.getRoles())); | ||
this.account = account; | ||
} | ||
|
||
private static Collection<? extends GrantedAuthority> authorities(Set<AccountRoles> roles) { | ||
return roles.stream().map(r -> new SimpleGrantedAuthority("ROLE_" + r.name())) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
public Account getAccount() { | ||
return account; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...01811restapi/src/main/java/me/whiteship/ksug201811restapi/accounts/AccountSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package me.whiteship.ksug201811restapi.accounts; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
||
import java.io.IOException; | ||
|
||
public class AccountSerializer extends JsonSerializer<Account> { | ||
@Override | ||
public void serialize(Account account, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | ||
jsonGenerator.writeStartObject(); | ||
jsonGenerator.writeNumberField("id", account.getId()); | ||
jsonGenerator.writeEndObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
ksug201811restapi/src/main/java/me/whiteship/ksug201811restapi/events/CurrentUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package me.whiteship.ksug201811restapi.events; | ||
|
||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : account") | ||
public @interface CurrentUser { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters