Skip to content

Commit b643bba

Browse files
committed
Fix: Resolving merge conflict
2 parents 17b477b + 425349f commit b643bba

File tree

16 files changed

+558
-26
lines changed

16 files changed

+558
-26
lines changed

build.gradle

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ project(':module-board') {
8383
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
8484

8585
testImplementation 'org.springframework.security:spring-security-test'
86+
87+
developmentOnly 'org.springframework.boot:spring-boot-devtools'
88+
implementation 'org.modelmapper:modelmapper:3.1.1'
89+
90+
8691
}
8792
}
8893

89-

module-board/src/main/java/com/boardapplication/BoardapplicationApplication.java

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.boardapplication.configuration;
2+
3+
import org.modelmapper.ModelMapper;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
public class AppConfig {
9+
@Bean
10+
public ModelMapper modelMapper() {
11+
return new ModelMapper();
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.boardapplication.configuration;
2+
3+
import com.boardapplication.dto.JoinDTO;
4+
import com.boardapplication.repository.UserRepository;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.stereotype.Component;
7+
import org.springframework.validation.Errors;
8+
import org.springframework.validation.Validator;
9+
10+
@Component
11+
@RequiredArgsConstructor
12+
public class JoinValidator implements Validator {
13+
14+
private final UserRepository userRepository;
15+
16+
@Override
17+
public boolean supports(Class<?> clazz) {
18+
return clazz.isAssignableFrom(JoinDTO.class);
19+
}
20+
21+
@Override
22+
public void validate(Object obj, Errors errors) {
23+
JoinDTO joinDTO = (JoinDTO) obj;
24+
if (userRepository.existsByUsername(joinDTO.getUsername())) {
25+
errors.rejectValue("username", "invalid.username",
26+
new Object[]{joinDTO.getUsername()}, "이미 사용중인 아이디입니다.");
27+
}
28+
}
29+
}
30+
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.boardapplication.configuration;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8+
import org.springframework.security.core.userdetails.UserDetailsService;
9+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
10+
import org.springframework.security.crypto.password.PasswordEncoder;
11+
import org.springframework.security.web.SecurityFilterChain;
12+
13+
@RequiredArgsConstructor
14+
@Configuration
15+
@EnableWebSecurity
16+
public class SecurityConfig {
17+
private final UserDetailsService userDetailsService;
18+
19+
@Bean
20+
public PasswordEncoder passwordEncoder() {
21+
return new BCryptPasswordEncoder();
22+
}
23+
24+
@Bean
25+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
26+
return http.csrf().disable()
27+
.authorizeRequests()
28+
.mvcMatchers("/", "/test", "/login", "/join", "/profile").permitAll()
29+
/*
30+
.mvcMatchers("/login").not().fullyAuthenticated() //
31+
.mvcMatchers("/user/**").authenticated()
32+
.mvcMatchers(HttpMethod.GET, "/user/{username}").authenticated()
33+
.mvcMatchers("/board/{username}").authenticated() //.access("hasRole('')")
34+
35+
*/
36+
.anyRequest().authenticated()
37+
.and()
38+
.formLogin()
39+
.usernameParameter("username")
40+
.passwordParameter("password")
41+
.loginProcessingUrl("/login")
42+
.defaultSuccessUrl("/")
43+
.and()
44+
.logout().logoutSuccessUrl("/")
45+
.and().build();
46+
}
47+
}
48+
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,88 @@
11
package com.boardapplication.controller;
22

3+
import com.boardapplication.configuration.JoinValidator;
4+
import com.boardapplication.dto.JoinDTO;
5+
import com.boardapplication.repository.UserRepository;
6+
import com.boardapplication.service.UserService;
7+
import com.core.entity.User;
8+
import lombok.RequiredArgsConstructor;
9+
import org.springframework.security.core.Authentication;
310
import org.springframework.stereotype.Controller;
11+
import org.springframework.ui.Model;
12+
import org.springframework.validation.Errors;
13+
import org.springframework.web.bind.WebDataBinder;
414
import org.springframework.web.bind.annotation.GetMapping;
15+
import org.springframework.web.bind.annotation.InitBinder;
16+
import org.springframework.web.bind.annotation.PostMapping;
17+
18+
import javax.validation.Valid;
519

620
@Controller
21+
@RequiredArgsConstructor
722
public class UserController {
8-
@GetMapping
9-
public String index() {
10-
return "test";
23+
24+
private final JoinValidator joinValidator;
25+
private final UserService userService;
26+
private final UserRepository userRepository;
27+
28+
@InitBinder("joinDTO")
29+
public void initBinder(WebDataBinder webDataBinder) {
30+
webDataBinder.addValidators(joinValidator);
31+
}
32+
33+
@GetMapping("/")
34+
public String index(Authentication authentication) {
35+
36+
if (authentication != null && authentication.isAuthenticated()) {
37+
return "redirect:/profile";
38+
} else {
39+
return "/login";
40+
}
41+
42+
}
43+
44+
45+
@GetMapping("/login")
46+
public String login(){
47+
return "login";
48+
}
49+
50+
51+
@GetMapping("/join")
52+
public String join(Model model) {
53+
model.addAttribute("joinDTO", new JoinDTO());
54+
return "/join";
1155
}
56+
57+
58+
59+
@PostMapping("/join")
60+
public String joinDTO(@Valid JoinDTO joinDTO, Errors errors) {
61+
if (errors.hasErrors()) {
62+
return "/join";
63+
}
64+
User user = userService.processNewUser(joinDTO);
65+
66+
return "/";
67+
}
68+
69+
/*
70+
@GetMapping("/checkusername")
71+
public ResponseEntity<String> checkUsername(@RequestParam String username) {
72+
if (userRepository.existsByUsername(username)) {
73+
return ResponseEntity.ok("이미 사용중인 아이디입니다.");
74+
} else {
75+
return ResponseEntity.ok("사용 가능한 아이디입니다.");
76+
}
77+
}
78+
79+
*/
80+
81+
82+
@GetMapping("/profile")
83+
public String profile() {
84+
return "profile";
85+
}
86+
87+
1288
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.boardapplication.dto;
2+
3+
import com.core.entity.User;
4+
import com.core.entity.Role;
5+
6+
import lombok.Data;
7+
import org.hibernate.validator.constraints.Length;
8+
import org.springframework.security.crypto.password.PasswordEncoder;
9+
10+
import javax.validation.constraints.NotEmpty;
11+
import javax.validation.constraints.Pattern;
12+
13+
@Data
14+
public class JoinDTO {
15+
@NotEmpty
16+
@Length(min = 3, max = 20)
17+
@Pattern(regexp = "^[a-zA-Z0-9]{3,20}$")
18+
private String username;
19+
20+
@NotEmpty
21+
@Length(min = 8, max = 60)
22+
private String password;
23+
24+
@NotEmpty
25+
@Pattern(regexp = "^[\\w._%+-]+@[\\w.-]+\\.[a-zA-Z]{2,}$")
26+
private String email;
27+
28+
@NotEmpty
29+
@Length(min = 3, max = 20)
30+
@Pattern(regexp = "^[a-zA-Z가-힣]{3,20}$")
31+
private String nickname;
32+
33+
public User toEntity(PasswordEncoder passwordEncoder) {
34+
User user = new User();
35+
user.setUsername(username);
36+
user.setEmail(email);
37+
user.setNickname(nickname);
38+
user.setRole(Role.NORMAL);
39+
40+
if (password != null) {
41+
String enPassword = passwordEncoder.encode(password);
42+
user.setPassword(enPassword);
43+
}
44+
45+
return user;
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.boardapplication.dto;
2+
3+
import com.core.entity.User;
4+
import lombok.Getter;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.security.core.GrantedAuthority;
7+
import org.springframework.security.core.userdetails.UserDetails;
8+
9+
import java.util.ArrayList;
10+
import java.util.Collection;
11+
12+
@Getter
13+
@RequiredArgsConstructor
14+
public class UserSessionDTO implements UserDetails {
15+
private final User user;
16+
17+
@Override
18+
public Collection<? extends GrantedAuthority> getAuthorities() {
19+
Collection<GrantedAuthority> authorities = new ArrayList<>();
20+
authorities.add(()-> String.valueOf(user.getRole()));
21+
return authorities;
22+
}
23+
24+
@Override
25+
public String getPassword() {
26+
return user.getPassword();
27+
}
28+
29+
@Override
30+
public String getUsername() {
31+
return user.getUsername();
32+
}
33+
34+
@Override
35+
public boolean isAccountNonExpired() {
36+
return true;
37+
}
38+
39+
@Override
40+
public boolean isAccountNonLocked() {
41+
return true;
42+
}
43+
44+
@Override
45+
public boolean isCredentialsNonExpired() {
46+
return true;
47+
}
48+
49+
@Override
50+
public boolean isEnabled() {
51+
return true;
52+
}
53+
}

module-board/src/main/java/com/boardapplication/repository/UserRepository.java

+2
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55

66
public interface UserRepository extends JpaRepository<User, Long> {
7+
boolean existsByUsername(String username);
8+
User findByUsername(String username);
79
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.boardapplication.service;
2+
3+
import com.boardapplication.dto.UserSessionDTO;
4+
import com.boardapplication.repository.UserRepository;
5+
import com.core.entity.User;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.security.core.userdetails.UserDetails;
8+
import org.springframework.security.core.userdetails.UserDetailsService;
9+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
10+
import org.springframework.stereotype.Service;
11+
12+
import javax.servlet.http.HttpSession;
13+
14+
@RequiredArgsConstructor
15+
@Service
16+
public class LoginService implements UserDetailsService {
17+
private final UserRepository userRepository;
18+
private final HttpSession session;
19+
20+
@Override
21+
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
22+
User user = userRepository.findByUsername(username);
23+
24+
if (user == null) {
25+
throw new UsernameNotFoundException(username);
26+
}
27+
session.setAttribute("username", username);
28+
return new UserSessionDTO(user);
29+
}
30+
}

0 commit comments

Comments
 (0)