로그인폼과 회원가입폼 HTML
더보기
로그인폼과 회원가입폼
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>로그인 페이지</title> |
| </head> |
| <body> |
| <h1>로그인 페이지</h1> |
| <hr> |
| <form action="/login" method="POST"> |
| <input type="text" name="username" placeholder="UserName"/><br/> |
| <input type="password" name="password" placeholder="Password"/><br/> |
| <button>로그인</button> |
| </form> |
| |
| <a href = "/joinForm">회원가입을 아직 않으셨나요?</a> |
| </body> |
| </html> |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>회원가입 페이지</title> |
| </head> |
| <body> |
| <h1>회원가입 페이지</h1> |
| <form action="/join" method="POST"> |
| <input type="text" name="username" placeholder="UserName"/><br/> |
| <input type="password" name="password" placeholder="Password"/><br/> |
| <input type="email" name="email" placeholder="Email"/><br/> |
| <button>회원가입</button> |
| </form> |
| |
| </body> |
| </html> |
[ 회원가입 Contoller ]
| @PostMapping("/join") |
| public String join(User user){ |
| |
| System.out.println(user); |
| |
| |
| |
| String rawPassword = user.getPassword(); |
| String encPassword = bCryptPasswordEncoder.encode(rawPassword); |
| user.setPassword(encPassword); |
| |
| |
| |
| |
| user.setRole("ROLE_USER"); |
| userRepository.save(user); |
| |
| |
| return "redirect:/loginForm"; |
| } |
[ USER MODEL ]
| package com.cos.security1.model; |
| |
| import lombok.Data; |
| import org.hibernate.annotations.CreationTimestamp; |
| |
| import javax.persistence.Entity; |
| import javax.persistence.GeneratedValue; |
| import javax.persistence.GenerationType; |
| import javax.persistence.Id; |
| import java.sql.Timestamp; |
| |
| @Entity |
| @Data |
| public class User { |
| |
| @Id |
| @GeneratedValue(strategy = GenerationType.IDENTITY) |
| private int id; |
| private String username; |
| private String password; |
| private String email; |
| private String role; |
| @CreationTimestamp |
| private Timestamp createdate; |
| |
| } |
[ USER REPORSITORY - JPA ]
| package com.cos.security1.repository; |
| |
| import com.cos.security1.model.User; |
| import org.springframework.data.jpa.repository.JpaRepository; |
| |
| |
| |
| |
| |
| public interface UserRepository extends JpaRepository<User, Integer> { |
| |
| |
| |
| |
| public User findByUsername(String username); |
| } |
[ config 수정 ]
밑 두줄 추가.
| |
| public class SecurityConfig extends WebSecurityConfigurerAdapter { |
| |
| @Override |
| protected void configure(HttpSecurity http) throws Exception { |
| |
| http.csrf().disable(); |
| |
| http.authorizeRequests() |
| .antMatchers("/user/**").authenticated() |
| .antMatchers("/manager/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')") |
| .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") |
| .anyRequest().permitAll() |
| .and() |
| .formLogin() |
| .loginPage("/loginForm") |
| .loginProcessingUrl("/login") |
| .defaultSuccessUrl("/"); |
| |
| |
| } |
[ userDetails를 상속하는 클래스 생성 ]
유저정보를 저장.
| package com.cos.security1.config.auth; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import com.cos.security1.model.User; |
| import org.springframework.security.core.GrantedAuthority; |
| import org.springframework.security.core.userdetails.UserDetails; |
| |
| import java.util.ArrayList; |
| import java.util.Collection; |
| |
| public class PrincipalDetails implements UserDetails { |
| |
| private User user; |
| |
| public PrincipalDetails(User user){ |
| this.user = user; |
| } |
| |
| |
| |
| @Override |
| public Collection<? extends GrantedAuthority> getAuthorities() { |
| Collection<GrantedAuthority> collect = new ArrayList<>(); |
| collect.add(new GrantedAuthority() { |
| @Override |
| public String getAuthority() { |
| return user.getRole(); |
| } |
| }); |
| return collect; |
| } |
| |
| @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; |
| } |
| } |
[ userDetailsService를 상속한 클래스 만들기 ]
이름 그대로 유저의 정보를 저장해주는 서비스
| package com.cos.security1.config.auth; |
| |
| import com.cos.security1.model.User; |
| import com.cos.security1.repository.UserRepository; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.security.core.userdetails.UserDetails; |
| import org.springframework.security.core.userdetails.UserDetailsService; |
| import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| import org.springframework.stereotype.Service; |
| |
| |
| |
| |
| |
| @Service |
| public class PrincaipalDetailsService implements UserDetailsService{ |
| |
| |
| @Autowired |
| private UserRepository userRepository; |
| |
| |
| @Override |
| public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
| |
| |
| |
| |
| |
| User userEntity = userRepository.findByUsername(username); |
| if(userEntity != null){ |
| return new PrincipalDetails(userEntity); |
| } |
| |
| return null; |
| } |
| } |
[ userRepository 에는 유저의 정보를 조회하는 부분 추가 ]
| |
| |
| public User findByUsername(String username); |