일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- 출처 따배도
- 파이썬sort
- 스프링부트중복예외처리
- 우분투도커설치
- 도커설치하는법
- 출처 문어박사
- 스프링부트구독취소
- 스프링부트팔로잉
- 인스타클론
- 스프링부트
- 스프링부트팔로우취소
- centos도커설치
- dockerinstall
- springboot_exception_handler
- 출처 노마드코더
- vm도커설치하는법
- 출처 메타코딩
- ssh도커설치
- 스프링부트사진올리기
- 스프링구독
- WAS웹서버
- 스프링사진업로드
- 스프링이미지업로드
- 스프링부트api
- 멀티폼
- 스프링부트서버에사진전송
- 스프링사진
- 서버에도커설치
- 출처 코딩셰프
- 스프링익셉션처리
- Today
- Total
MakerHyeon
[springBoot] 로그인 기능 구현 본문
로그인 기능 구현
1. POST 로그인요청
- 원래 SELECT할때는 GET을 쓰지만,로그인을 할때는 POST를 쓴다.
- GET은 주소창에 데이터를 노출시켜서,로그인요청을 할때는 Data를 Body에 안고 가기 위해 POST사용
<!--signin.jsp-->
<!--로그인 인풋-->
<form class="login__input" action="/auth/signin" method="POST">
<input type="text" name="username" placeholder="유저네임" required="required" />
<input type="password" name="password" placeholder="비밀번호" required="required" />
<button>로그인</button>
</form>
2. loginProcessingUrl에 '로그인 요청 url' 등록
// SecurityConfig.java
@EnableWebSecurity // 해당 파일로 시큐리티를 활성화
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public BCryptPasswordEncoder encode(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//super.configure(http); //기존 시큐리티가 가지고 있는 기능이 다 비활성화 됨
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/","/user/**","/image/**","/subscribe/**","/comment/**")
.authenticated() //인증필요
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/auth/signin") //인증필요시 해당페이지로이동 // GET
.loginProcessingUrl("/auth/signin") // POST ->스프링시큐리티가 로그인진행
.defaultSuccessUrl("/");
}
}
3. JPA query method 생성
// UserRepository.java
public interface UserRepository extends JpaRepository<User,Integer> {
User findByUsername(String username);
}
4. PrincipalDetailsService 생성 후 UserDetailsService implements
- Password는 Security가 알아서 체킹하므로 신경 쓸 필요가 없다.
- Return이 잘되면,자동으로 UserDetails type session을 만든다.
// PrincipalDetailsService.java
@RequiredArgsConstructor
@Service
public class PrincipalDetailsService implements UserDetailsService {
private final UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User userEntity = userRepository.findByUsername(username);
if(userEntity == null){
return null;
}else{
//세션에 저장될때 세션에저장된 유저오브젝터 활용할 수 있게 한다.
return new PrincipalDetails(userEntity);
}
}
}
5. PrincipalDetails.java 파일 생성(유저오브젝터 세션저장)
// PrincipalDetails.java
@Data
public class PrincipalDetails implements UserDetails {
private static final long serialVersionUID = 1L;
private User user;
public PrincipalDetails(User user) {
this.user = user;
}
// 권한 : 한개가 아닐 수 있음 (ex_3개 이상의 권한)
@Override
public Collection<? extends GrantedAuthority> getAuthorities() { //권한
Collection<GrantedAuthority> collector = new ArrayList<>();
collector.add(()->{return user.getRole();});
return collector;
}
@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;
}
}
Feat. logout
-로그아웃시 security가 자동으로 세션을 끊어준다.
<button onclick="location.href='/logout'">로그아웃</button>
- Spring Data Query Creation 자세히 알아보기
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
Spring Data JPA - Reference Documentation
Example 119. Using @Transactional at query methods @Transactional(readOnly = true) interface UserRepository extends JpaRepository { List findByLastname(String lastname); @Modifying @Transactional @Query("delete from User u where u.active = false") void del
docs.spring.io
'SpringBoot' 카테고리의 다른 글
[springBoot] 회원정보수정(Ajax,유효성검사) (0) | 2023.01.06 |
---|---|
[springBoot] Security Tag library (0) | 2023.01.05 |
[springBoot] 유효성 검사,글로벌 예외처리 (0) | 2023.01.05 |
[springBoot] 간단한 회원가입 기능 구현과 DB연동 (0) | 2023.01.05 |
[springBoot] controller 기본개념 정리 (0) | 2023.01.04 |