Skip to content

Commit

Permalink
simplify project
Browse files Browse the repository at this point in the history
Signed-off-by: Kirill Mokevnin <[email protected]>
  • Loading branch information
mokevnin committed Oct 15, 2023
1 parent c83122f commit 05323fa
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void run(ApplicationArguments args) throws Exception {
var email = "[email protected]";
var userData = new User();
userData.setEmail(email);
userData.setPassword("qwerty");
userData.setPasswordDigest("qwerty");
userService.createUser(userData);

var user = userRepository.findByEmail(email).get();
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/hexlet/blog/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;

import io.hexlet.blog.service.CustomUserDetailsService;
import lombok.AllArgsConstructor;

@Configuration
@EnableWebSecurity
@AllArgsConstructor
public class SecurityConfig {
@Autowired
private final JwtDecoder jwtDecoder;
private JwtDecoder jwtDecoder;

@Autowired
private final PasswordEncoder passwordEncoder;
private PasswordEncoder passwordEncoder;

@Autowired
private CustomUserDetailsService userService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public class AuthenticationController {
@PostMapping("/login")
public String create(@RequestBody AuthRequest authRequest) {
var authentication = new UsernamePasswordAuthenticationToken(
authRequest.username(), authRequest.password());
authRequest.getUsername(), authRequest.getPassword());

authenticationManager.authenticate(authentication);

var token = jwtUtils.generateToken(authRequest.username());
var token = jwtUtils.generateToken(authRequest.getUsername());
return token;
}
}
9 changes: 8 additions & 1 deletion src/main/java/io/hexlet/blog/dto/AuthRequest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package io.hexlet.blog.dto;

public record AuthRequest(String username, String password) {
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class AuthRequest {
private String username;
private String password;
}
5 changes: 2 additions & 3 deletions src/main/java/io/hexlet/blog/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public class User implements UserDetails, BaseEntity {
@ToString.Include
private String lastName;

@NotBlank
private String password;
private String passwordDigest;

@LastModifiedDate
private Date updatedAt;
Expand All @@ -71,7 +70,7 @@ public class User implements UserDetails, BaseEntity {

@Override
public String getPassword() {
return password;
return passwordDigest;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public UserDetails loadUserByUsername(String email) throws UsernameNotFoundExcep
public void createUser(UserDetails userData) {
var user = new User();
user.setEmail(userData.getUsername());
String hashedPassword = passwordEncoder.encode(userData.getPassword());
user.setPassword(hashedPassword);
var hashedPassword = passwordEncoder.encode(userData.getPassword());
user.setPasswordDigest(hashedPassword);
userRepository.save(user);
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/hexlet/blog/util/JWTUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import org.springframework.security.oauth2.jwt.JwtEncoder;
import org.springframework.security.oauth2.jwt.JwtEncoderParameters;
import org.springframework.stereotype.Component;
import lombok.AllArgsConstructor;

@Component
@AllArgsConstructor
public class JWTUtils {

@Autowired
Expand Down

0 comments on commit 05323fa

Please sign in to comment.