728x90
@Service
@Slf4j
@AllArgsConstructor
public class CustomUserServiceImp implements UserDetailsService {

  private final UserRepository userRepository;

  private final AuthoritiesRepository authoritiesRepository;

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    User user = userRepository.findByUsername(username);
    if ("null".equals(user)) {
      throw new UsernameNotFoundException("not found user:" + username);
    }

    CustomUser customUser = new CustomUser();
    customUser.setUsername(user.getUsername());
    customUser.setPassword(user.getPassword());
    customUser.setAuthorities(getAuthorities(username));
    customUser.setEnabled(true);
    customUser.setAccountNonExpired(true);
    customUser.setAccountNonLocked(true);
    customUser.setCredentialsNonExpired(true);
    return customUser;
  }

  public Collection<GrantedAuthority> getAuthorities(String username) {
    List<Authority> authList = authoritiesRepository.findByUsername(username);
    List<GrantedAuthority> authorities = new ArrayList<>();
    for (Authority authority : authList) {
      authorities.add(new SimpleGrantedAuthority(authority.getAuthority()));
    }
    return authorities;
  }
}

내저장소 바로가기 luxury515

'Springboot3.0' 카테고리의 다른 글

[Interface]`UserDetails` 구현  (0) 2023.04.16
Springboot Security  (0) 2023.04.16
728x90
@Data
public class CustomUser implements UserDetails {
  private String username;
  private String password;
  private boolean isEnabled;
  private boolean isAccountNonExpired;
  private boolean isAccountNonLocked;
  private boolean isCredentialsNonExpired;
  private Collection<? extends GrantedAuthority> authorities;
}


내저장소 바로가기 luxury515

'Springboot3.0' 카테고리의 다른 글

[Interface]`UserDetailsService`  (0) 2023.04.16
Springboot Security  (0) 2023.04.16
728x90
@EnableWebSecurity
@Configuration
@AllArgsConstructor
public class SecurityConfig {

  private final CustomUserServiceImp customUserServiceImp;
  @Bean
  public BCryptPasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }

  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests(
            (auth) ->
                auth.antMatchers("/user/**", "/auth/**").permitAll().anyRequest().authenticated())
        .httpBasic(withDefaults());
    http.headers()
            .frameOptions().sameOrigin();
    http.userDetailsService(customUserServiceImp);
    return http.build();
  }

  @Bean
  public WebSecurityCustomizer webSecurityCustomizer() {
    return (web -> web.ignoring().antMatchers("/images/**", "/js/**", "/webjars/**"));
  }
}


내저장소 바로가기 luxury515

'Springboot3.0' 카테고리의 다른 글

[Interface]`UserDetailsService`  (0) 2023.04.16
[Interface]`UserDetails` 구현  (0) 2023.04.16
728x90

현재 Spring에서 아래와 같이 3가지 DI 방식을 사용하고 있다.

  • 가장 흔한 필드 주입방식: Field Injection 방식 (비 권장)
    @Controller
    public class BoardController {
    	@Autowired 
      private IBoardItemService boardItemService;
    }
  • 수정자 주입: Setter Injection
    private IBoardItemService boardItemService;
      @Autowired // 4.3 부터 생략 가능
      public void SetIBoardItemService (MessageSource messageSource){
        this.messageSource = messageSource;
      }
  • 생성자 주입: 가장 선호하는 방식 ( Class 상단에 @RequiredArgsConstructor 추가 , @AllArgsConstructor 는 지운다!)
    @RequiredArgsConstructor
    public class BoardController {
    	private final IBoardItemService boardItemService;
    }

    Lombok 없이 사용시:

    
    public class BoardController {
    	private final IBoardItemService boardItemService; // Immutability 이슈까지 해결하고 싶다면 private final 접근자 붙여주기
        
    		@Autowired
    		public BoardController(IBoardItemService boardItemService) {
        	this.boardItemService = boardItemService;
        }
    } 

내저장소 바로가기 luxury515

'Back-end > 기타' 카테고리의 다른 글

on duplicate key update  (0) 2023.04.16
AWS KMS 관련 세팅  (0) 2023.04.14
plantUml preview 갑자기 작동안될때.  (0) 2023.04.14
gradle bootrun 작동 에러  (0) 2023.04.14
radius(레이디스)서버  (0) 2023.04.14

+ Recent posts