Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverted the @AuthenticationPrincipal change #45

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package pl.simpleascoding.tutoringplatform.api.publicResource;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import pl.simpleascoding.tutoringplatform.domain.user.User;

import java.security.Principal;

@RestController
class TestApi {
Expand All @@ -14,7 +14,7 @@ public String testAdmin() {
}

@GetMapping("/testUser")
public String testUser(@AuthenticationPrincipal User user) {
return String.format("Hi %s %s, you are a USER", user.getName(), user.getSurname());
public String testUser(Principal principal) {
return String.format("Hello %s", principal.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import pl.simpleascoding.tutoringplatform.domain.user.User;
import pl.simpleascoding.tutoringplatform.dto.ChangeUserPasswordDTO;
import pl.simpleascoding.tutoringplatform.dto.CreateUserDTO;
import pl.simpleascoding.tutoringplatform.service.user.UserFacade;

import javax.servlet.http.HttpServletRequest;

import java.security.Principal;

@RestController
Expand All @@ -32,9 +29,9 @@ ResponseEntity<String> confirmRegistration(@RequestParam String tokenValue) {
}

@PostMapping("/change-password")
ResponseEntity<String> changeUserPassword(@RequestBody ChangeUserPasswordDTO dto, @AuthenticationPrincipal User loggedInUser) {
ResponseEntity<String> changeUserPassword(@RequestBody ChangeUserPasswordDTO dto, Principal principal) {

return new ResponseEntity<>(userFacade.changeUserPassword(dto, loggedInUser), HttpStatus.OK);
return new ResponseEntity<>(userFacade.changeUserPassword(dto, principal.getName()), HttpStatus.OK);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Except

httpSecurity.authorizeRequests().antMatchers("/testAdmin").hasAnyAuthority(RoleType.ADMIN.toString());
httpSecurity.authorizeRequests().antMatchers("/testUser").hasAnyAuthority(RoleType.USER.toString());
httpSecurity.authorizeRequests().antMatchers("/change-password").hasAnyAuthority(RoleType.USER.toString());
httpSecurity.authorizeRequests().antMatchers("/api/v1/users/change-password").hasAnyAuthority(RoleType.USER.toString());
httpSecurity.authorizeRequests().anyRequest().permitAll();
return httpSecurity.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
UserDetails userDetails = userDetailsService.loadUserByUsername(jwt.getSubject());

SecurityContextHolder.getContext()
.setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()));
.setAuthentication(new UsernamePasswordAuthenticationToken(jwt.getSubject(), null, userDetails.getAuthorities()));
filterChain.doFilter(request, response);
} catch (RuntimeException ex) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import pl.simpleascoding.tutoringplatform.domain.user.User;
import pl.simpleascoding.tutoringplatform.dto.ChangeUserPasswordDTO;
import pl.simpleascoding.tutoringplatform.dto.CreateUserDTO;

Expand All @@ -24,9 +23,9 @@ public String confirmUserRegistration(String tokenValue) {

}

public String changeUserPassword(ChangeUserPasswordDTO dto, User user) {
public String changeUserPassword(ChangeUserPasswordDTO dto, String username) {

return userService.changeUserPassword(dto, user);
return userService.changeUserPassword(dto, username);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pl.simpleascoding.tutoringplatform.service.user;

import org.springframework.stereotype.Service;
import pl.simpleascoding.tutoringplatform.domain.user.User;
import pl.simpleascoding.tutoringplatform.dto.ChangeUserPasswordDTO;
import pl.simpleascoding.tutoringplatform.dto.CreateUserDTO;

Expand All @@ -12,6 +11,6 @@ interface UserService {

String confirmUserRegistration(String token);

String changeUserPassword(ChangeUserPasswordDTO dto, User user);
String changeUserPassword(ChangeUserPasswordDTO dto, String username);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -90,13 +91,16 @@ public String confirmUserRegistration(String tokenValue) {
* The password is first checked for correctness of the entered data by the user.
* Then the currently specified password is checked against the one in storage.
*
* @param newPasswordsData User-provided multiple password data
* @param userEntity Users entity
* @return The status of the operation
* @param newPasswordsData User-provided multiple password data
* @param username Username of the user
* @return The status of the operation
*/
@Override
@Transactional
public String changeUserPassword(ChangeUserPasswordDTO newPasswordsData, User userEntity) {
public String changeUserPassword(ChangeUserPasswordDTO newPasswordsData, String username) {
User userEntity = userRepository.findUserByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException(username));

if (isChangeAllowed(userEntity.getPassword(), newPasswordsData)) {
userEntity.setPassword(passwordEncoder.encode(newPasswordsData.newPassword()));

Expand Down