Skip to content

Commit

Permalink
[FEATURE] E4-S3 회원가입 및 테스트 #25
Browse files Browse the repository at this point in the history
  • Loading branch information
HyeJeongIm committed Oct 17, 2021
1 parent d6d3cf0 commit b257266
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 208 deletions.
4 changes: 2 additions & 2 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-security' // 필요할 때 주석 해제하고 사용
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation group: 'com.github.ulisesbocchio', name: 'jasypt-spring-boot-starter', version: '3.0.3'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
Expand All @@ -42,4 +42,4 @@ test {
bootRun {
String activeProfile = System.properties['spring.profiles.active']
systemProperty "spring.profiles.active", activeProfile
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package com.infp.ciat.config.security;

import com.infp.ciat.user.service.AccountService;
//import com.infp.ciat.user.service.OAuth2DetailesService;
//import com.infp.ciat.user.service.OAuth2DetailesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

/***
* 스프링시큐리티 설정
*/

@Transactional(readOnly = true)
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AccountService customUserDetailService;

// @Autowired
// private OAuth2DetailesService oAuth2DetailesService;

/***
* default 패스워드 암호화알고리즘 사용 설정
Expand All @@ -38,37 +40,20 @@ public PasswordEncoder passwordEncoder(){
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
Loginfilter loginfilter = new Loginfilter(authenticationManagerBean());
loginfilter.setFilterProcessesUrl("/signin");

http.csrf().disable();
http
.authorizeRequests()
.anyRequest().permitAll()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable()
.addFilter(loginfilter);
}

/***
* 사용자 userdetailservice 등록
* @param auth
* @throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserDetailService);
}
// .antMatchers("/user/**").authenticated() // Q
.anyRequest().permitAll()
.and()
.formLogin()
.usernameParameter("email")
.passwordParameter("password");

/***
* login 필터를 위한 authenticationManager Bean으로 등록
* @return
* @throws Exception
*/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
//
// http
// .oauth2Login()
// .userInfoEndpoint()
// .userService(oAuth2DetailesService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ public class AccountController {
private final AccountService accountService;

@PostMapping("/signup")
public ResponseEntity<SignUpResponse> SignUp(@Valid @RequestBody SignupRequestDTO requestDTO) {
public ResponseEntity<SignUpResponse> signUp(@Valid @RequestBody SignupRequestDTO requestDTO) {

log.info("------------------- 회원가입 요청 -----------------------");

Long created_id = accountService.SignUp(requestDTO);
Long created_id = accountService.signUp(requestDTO);
return new ResponseEntity<>(new SignUpResponse(created_id), HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.infp.ciat.user.controller.dto.request;

import com.infp.ciat.user.entity.Account;
import com.infp.ciat.user.entity.Role;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;

@Getter
@Setter
@NoArgsConstructor
public class SignupRequestDTO {

@NotEmpty
private String email;

Expand All @@ -16,4 +22,21 @@ public class SignupRequestDTO {

@NotEmpty
private String nickname;

@Builder
public SignupRequestDTO(String email, String password, String nickname) {
this.email = email;
this.password = password;
this.nickname = nickname;
}

public Account toEntity() {
return Account.builder()
.email(email)
.password(password)
.nickname(nickname)
.role(Role.ROLE_USER)
.build();
}

}
12 changes: 9 additions & 3 deletions backend/src/main/java/com/infp/ciat/user/entity/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
/***
* 유저 Entity
*/
@Builder

@NoArgsConstructor
@Setter
@Getter
@Entity
@ToString
public class Account extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -31,9 +33,13 @@ public class Account extends BaseTimeEntity {
@Column(nullable = false)
private Role role;

// private String provider;
// private String providerId;



@Builder
public Account(Long id, String email, String nickname, String password, Role role) {
this.id = id;
public Account(String email, String nickname, String password, Role role) {
this.email = email;
this.nickname = nickname;
this.password = password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
import java.util.Optional;

public interface AccountRepository extends JpaRepository<Account, Long> {
Optional<Account> findByEmail(String email);

// select * from account where email = ?
Account findByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
package com.infp.ciat.user.service;

import com.infp.ciat.user.controller.dto.AccountContext;
import com.infp.ciat.user.controller.dto.request.SignupRequestDTO;
import com.infp.ciat.user.entity.Account;
import com.infp.ciat.user.entity.Role;
import com.infp.ciat.user.repository.AccountRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
@Slf4j
public class AccountService implements UserDetailsService {
public class AccountService {

private final AccountRepository accountRepository;

private final PasswordEncoder passwordEncoder;



/***
* 회원가입서비스
* 회원중복검사는 JPA가 수행하여 생략
* Role은 default로 USER
* @param requestdto
*/
@Transactional
public Long SignUp(SignupRequestDTO requestdto){
Account new_account = Account.builder()
.email(requestdto.getEmail())
.nickname(requestdto.getNickname())
.password(passwordEncoder.encode(requestdto.getPassword()))
.role(Role.ROLE_USER)
.build();

return accountRepository.save(new_account).getId();
}
public Long signUp(SignupRequestDTO requestdto){

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Account find_user = accountRepository.findByEmail(email)
.orElseThrow(() -> new UsernameNotFoundException("사용자가 없습니다"));
log.debug(String.format("%s 계정 로그인 시도", email));
return AccountContext.FromAccountToAccountContext(find_user);
requestdto.setPassword(passwordEncoder.encode(requestdto.getPassword()));
Account accountEntity = requestdto.toEntity();

return accountRepository.save(accountEntity).getId();
}

}

This file was deleted.

Loading

0 comments on commit b257266

Please sign in to comment.