-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from saneci/feature/authorization
Authorization and authentication
- Loading branch information
Showing
38 changed files
with
980 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/ru/saneci/booklibrary/config/SecurityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ru.saneci.booklibrary.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfiguration { | ||
|
||
@Bean | ||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
return http | ||
.authorizeHttpRequests(authorisationCustomizer -> authorisationCustomizer | ||
.requestMatchers("/admin/**").hasAnyRole("ADMIN", "MANAGER") | ||
.requestMatchers("/auth/**", "/error").permitAll() | ||
.anyRequest().hasAnyRole("USER", "ADMIN", "MANAGER") | ||
) | ||
.formLogin(loginCustomizer -> loginCustomizer | ||
.loginPage("/auth/login") | ||
.loginProcessingUrl("/auth/login/process") | ||
.defaultSuccessUrl("/", true) | ||
.failureUrl("/auth/login?error") | ||
) | ||
.logout(logoutCustomizer -> logoutCustomizer | ||
.logoutUrl("/logout") | ||
.logoutSuccessUrl("/auth/login") | ||
) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/ru/saneci/booklibrary/controller/AdminController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ru.saneci.booklibrary.controller; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
@Controller | ||
@RequestMapping("/admin") | ||
public class AdminController { | ||
|
||
@GetMapping("/dashboard") | ||
public String getDashboard() { | ||
return "admin/dashboard"; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/ru/saneci/booklibrary/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ru.saneci.booklibrary.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import ru.saneci.booklibrary.domain.Person; | ||
import ru.saneci.booklibrary.domain.Role; | ||
import ru.saneci.booklibrary.service.PersonService; | ||
import ru.saneci.booklibrary.util.BindingResultLogger; | ||
import ru.saneci.booklibrary.util.PersonValidator; | ||
|
||
@Controller | ||
@RequestMapping("/auth") | ||
public class AuthController { | ||
|
||
private final Logger log = LoggerFactory.getLogger(AuthController.class); | ||
private final BindingResultLogger brLogger = BindingResultLogger.getLogger(AuthController.class); | ||
private final PersonValidator personValidator; | ||
private final PersonService personService; | ||
|
||
@Autowired | ||
public AuthController(PersonValidator personValidator, PersonService personService) { | ||
this.personValidator = personValidator; | ||
this.personService = personService; | ||
} | ||
|
||
@GetMapping("/login") | ||
public String getLoginPage() { | ||
return "auth/login"; | ||
} | ||
|
||
@GetMapping("/registration") | ||
public String getRegistrationPage(@ModelAttribute("person") Person person) { | ||
return "auth/registration"; | ||
} | ||
|
||
@PostMapping("/registration") | ||
public String performRegistration(@ModelAttribute("person") @Valid Person person, BindingResult bindingResult) { | ||
log.debug("performRegistration: start processing"); | ||
personValidator.validate(person, bindingResult); | ||
if (bindingResult.hasErrors()) { | ||
brLogger.warn("performRegistration", bindingResult); | ||
return "auth/registration"; | ||
} | ||
// TODO: remove setRole after https://github.com/users/saneci/projects/3/views/1?pane=issue&itemId=56174894 | ||
person.setRole(Role.ROLE_USER); | ||
personService.save(person); | ||
log.debug("performRegistration: finish processing"); | ||
|
||
return "redirect:/auth/login"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.saneci.booklibrary.domain; | ||
|
||
public enum Role { | ||
|
||
// TODO: remove ROLE_READER after https://github.com/users/saneci/projects/3/views/1?pane=issue&itemId=56174894 | ||
ROLE_ADMIN, ROLE_USER, ROLE_MANAGER, ROLE_READER | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/ru/saneci/booklibrary/security/PersonDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ru.saneci.booklibrary.security; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import ru.saneci.booklibrary.domain.Person; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class PersonDetails implements UserDetails { | ||
|
||
private final transient Person person; | ||
|
||
public PersonDetails(Person person) { | ||
this.person = person; | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return List.of(new SimpleGrantedAuthority(person.getRole().name())); | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return this.person.getPassword(); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return this.person.getUsername(); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
|
||
public Person getPerson() { | ||
return person; | ||
} | ||
} |
Oops, something went wrong.