시큐리티를 공부하다가 생소한 표현이 있어서 찾아보게되었다.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>();
user.getRoleList().forEach(r->{
authorities.add(()->r);
});
return authorities;
}
authorities.add(()->r);
람다 표현식을 자주 사용하왔지만 Collection에 쓰는 것은 처음이였기 때문이다.
먼저 GrantedAuthority에 대해서 알아야한다.
해당 클래스는 인터페이스다. 인터페이스를 사용하기 위해서는 구현체 클래스가 필요하다.
public interface GrantedAuthority extends Serializable {
String getAuthority();
}
람다를 사용하지 않고 일반적으로 구현한다면 다음과 같다.
직접 구현클래스르 작성하는 것이다.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>();
user.getRoleList().forEach(r->{
authorities.add(new GrantedTest(r));
});
return authorities;
}
public static class GrantedTest implements GrantedAuthority{
private String auth;
public GrantedTest(String auth) {
this.auth = auth;
}
@Override
public String getAuthority() {
return auth;
}
}
익명 클래스를 통해서 코드를 간소화 할 수 있다.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>();
user.getRoleList().forEach(r->{
authorities.add(new GrantedAuthority() {
@Override
public String getAuthority() {
return null;
}
});
});
return authorities;
}
익명 클래스는 인터페이스 구현클래스를 따로 생성하지 않고 인터페이스를 구현할 수 있기 때문에 매우 유용하다.
이 코드를 더 간소화 할 수 있다.
-> 람다를 사용하면 가능하다.
하지만 주의해야할 것은 람다를 통해서 인터페이스를 구현할때는 오버라이드 해야하는 메소드가 하나여야만 한다.
위와 같은 케이스는 오버라이드해야하는 메소드가 하나이기때문에 람다를 통해서 간소화가 가능하다.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>();
user.getRoleList().forEach(r->
authorities.add(() -> r)
);
return authorities;
}
'2024' 카테고리의 다른 글
[노마더 코드] 7.0.2 마이그레이션 (0) | 2024.12.09 |
---|---|
[Spring-Security] 스프링 시큐리티 Exception 과정 (로그인과 연관있음) (0) | 2024.01.22 |
[Spring-Security] 아키텍처 - 필터 (0) | 2024.01.22 |
[JWT] 사전 지식 - HMAC, RSA, SHA256, HS256, RS256 (0) | 2024.01.19 |
[Security] CIA (0) | 2024.01.17 |
시큐리티를 공부하다가 생소한 표현이 있어서 찾아보게되었다.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>();
user.getRoleList().forEach(r->{
authorities.add(()->r);
});
return authorities;
}
authorities.add(()->r);
람다 표현식을 자주 사용하왔지만 Collection에 쓰는 것은 처음이였기 때문이다.
먼저 GrantedAuthority에 대해서 알아야한다.
해당 클래스는 인터페이스다. 인터페이스를 사용하기 위해서는 구현체 클래스가 필요하다.
public interface GrantedAuthority extends Serializable {
String getAuthority();
}
람다를 사용하지 않고 일반적으로 구현한다면 다음과 같다.
직접 구현클래스르 작성하는 것이다.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>();
user.getRoleList().forEach(r->{
authorities.add(new GrantedTest(r));
});
return authorities;
}
public static class GrantedTest implements GrantedAuthority{
private String auth;
public GrantedTest(String auth) {
this.auth = auth;
}
@Override
public String getAuthority() {
return auth;
}
}
익명 클래스를 통해서 코드를 간소화 할 수 있다.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>();
user.getRoleList().forEach(r->{
authorities.add(new GrantedAuthority() {
@Override
public String getAuthority() {
return null;
}
});
});
return authorities;
}
익명 클래스는 인터페이스 구현클래스를 따로 생성하지 않고 인터페이스를 구현할 수 있기 때문에 매우 유용하다.
이 코드를 더 간소화 할 수 있다.
-> 람다를 사용하면 가능하다.
하지만 주의해야할 것은 람다를 통해서 인터페이스를 구현할때는 오버라이드 해야하는 메소드가 하나여야만 한다.
위와 같은 케이스는 오버라이드해야하는 메소드가 하나이기때문에 람다를 통해서 간소화가 가능하다.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>();
user.getRoleList().forEach(r->
authorities.add(() -> r)
);
return authorities;
}
'2024' 카테고리의 다른 글
[노마더 코드] 7.0.2 마이그레이션 (0) | 2024.12.09 |
---|---|
[Spring-Security] 스프링 시큐리티 Exception 과정 (로그인과 연관있음) (0) | 2024.01.22 |
[Spring-Security] 아키텍처 - 필터 (0) | 2024.01.22 |
[JWT] 사전 지식 - HMAC, RSA, SHA256, HS256, RS256 (0) | 2024.01.19 |
[Security] CIA (0) | 2024.01.17 |