- 비밀번호의 암호화를 제공함
- 평문 지원기능은 Spring5 에서 Deprecated 됨
생성 : PasswordEncoderFactories
- PasswordEncoder passwordEncoder = PassswordEncoderFactories.createDelegatingPasswordEncoder()
- 여러개의 PasswordEncoder 명단에서 상황에 맞게 사용하게끔 지원하는 클래스
포멧 : {암호화알고리즘}
- 기본 포멧은 Bscrypt 로 가장 많이 쓰임
- {bcrypt}~~~~ 의 형식으로 인코딩
PasswordEncoder 인터페이스의 메서드
- encode(password) : 패스워드의 암호화를 진행
- boolean matches(rawpassword, encodedPassword) : 평문 값과 암호화된 패스워드의 비교
번외 : jpa 에 대해 조금은 알게 됨
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
#postgres 스키마, 유저명 , 암호 설정
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
# jpa 설정
# spring.jpa.hibernate.ddl-auto=create JPA Entity클래스에 대해서 자동적으로 테이블을 만드는 속성
# spring.jpa.properties.hibernate.format_sql=true 로그를 읽기 쉽게 포멧
# spring.jpa.properties.hibernate.show_sql=true 콘솔에 sql 문이 찍히게 함
spring.thymeleaf.cache=false
spring.devtools.livereload.enabled=true
spring.devtools.restart.enabled=true
#서버 재시작을 안해도 변경사항을 확인가능
spring.main.allow-bean-definition-overriding=true
#spring.profiles.active=pointcut
save(Entity) 메서드는 insert 문을 생성
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Transactional
@Override
public void createUser(Account account) {
userRepository.save(account); // Account 객체를 저장
}
}
들어갈 타입은 @Entity 명시객체이므로, modelMapper를 사용했던 것이라고 분석함,
ModelMapper를 생성하여 DTO 를 Entity 타입 클래스로 맵핑해주고있음.
@PostMapping(value = "/users")
public String createUser(AccountDTO accountDTO){
ModelMapper modelMapper = new ModelMapper();
Account account = modelMapper.map(accountDTO, Account.class); // 해당 객체를 Account 클래스로 맵핑 시킴
account.setPassword(passwordEncoder.encode(account.getPassword()));// 가입시 비밀번호 암호화
userService.createUser(account);//등록
return "redirect:/";
}
JpaRepository 의 상속, <만들어 낼 Entity , PK 타입> 으로 명시
import com.example.securityapp.domain.Account;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<Account, Long> {// JPA 인터페이스 구현, 만들 Entity 와 ID 타입이 들어감
}
Entity 를 만들어내는 어노테이션, @Id는 PK,
@GeneratedValue는 주키의 값을 위한 자동 생성 전략을 명시하는데 사용한다. 선택적 속성으로 generator와 strategy
가 있음
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@Data
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.AUTO) // AcoountEntity가 생성됨
private Long Id;
private String username;
private String password;
private String email;
private String age;
private String role;
}
'springframework > 시작하자SpringSecurity' 카테고리의 다른 글
27.인증구현, CustomAuthenticationProvider (0) | 2020.09.27 |
---|---|
26.인증 프로세스 구현, CutonUserDetailsService (0) | 2020.09.27 |
25.정적 자원 관리 - WebIgnore 설정 (0) | 2020.09.27 |
24.SpringSecurity 필터, 아키텍쳐 정리 (0) | 2020.09.23 |
23.AccessDesicionManager, AccessDecisionVotor (0) | 2020.09.22 |