Нормализованы методы под basic авторизацию. Но все еще без ролей

This commit is contained in:
Daniil Makeev 2025-02-19 17:25:15 +03:00
parent 99d6c53b90
commit fd63fd182f
8 changed files with 24 additions and 17 deletions

View File

@ -5,12 +5,14 @@ import com.example.onomatopoeiaback.domain.employee.Employee;
import com.example.onomatopoeiaback.domain.employee.EmployeeDTO; import com.example.onomatopoeiaback.domain.employee.EmployeeDTO;
import com.example.onomatopoeiaback.domain.visit.Visit; import com.example.onomatopoeiaback.domain.visit.Visit;
import com.example.onomatopoeiaback.domain.visit.VisitDTO; import com.example.onomatopoeiaback.domain.visit.VisitDTO;
import com.example.onomatopoeiaback.security.Auth;
import com.example.onomatopoeiaback.service.EmployeeService; import com.example.onomatopoeiaback.service.EmployeeService;
import com.example.onomatopoeiaback.service.VisitService; import com.example.onomatopoeiaback.service.VisitService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@ -28,27 +30,27 @@ public class EmployeeController {
@PostMapping("/create") @PostMapping("/create")
@SecurityRequirement(name = "basicAuth") @SecurityRequirement(name = "basicAuth")
public ResponseEntity<Employee> createEmployee(@RequestBody EmployeeDTO employeeDTO) { public ResponseEntity<Employee> createEmployee(Authentication authentication, @RequestBody EmployeeDTO employeeDTO) {
return ResponseEntity.ok(employeeService.createEmployee(employeeDTO)); return ResponseEntity.ok(employeeService.createEmployee(employeeDTO));
} }
@GetMapping("/{username}/info") @GetMapping("/info")
@SecurityRequirement(name = "basicAuth") @SecurityRequirement(name = "basicAuth")
public ResponseEntity<Employee> info(@PathVariable String username) { public ResponseEntity<Employee> info(Authentication authentication) {
return ResponseEntity.ok(employeeService.info(username)); return ResponseEntity.ok(employeeService.info(Auth.getEmployee(authentication).getLogin()));
} }
@GetMapping("/{username}/auth") @GetMapping("/auth")
@SecurityRequirement(name = "basicAuth") @SecurityRequirement(name = "basicAuth")
public ResponseEntity<Visit> auth(@PathVariable String username) { public ResponseEntity<Visit> auth(Authentication authentication) {
employeeService.auth(username); employeeService.auth(Auth.getEmployee(authentication).getLogin());
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
@PatchMapping("/{username}/open") @PatchMapping("/open")
@SecurityRequirement(name = "basicAuth") @SecurityRequirement(name = "basicAuth")
public ResponseEntity<Visit> open(@PathVariable String username, @RequestBody VisitDTO visitDTO) { public ResponseEntity<Visit> open(Authentication authentication, @RequestBody VisitDTO visitDTO) {
visitService.register(username, visitDTO); visitService.register(Auth.getEmployee(authentication).getLogin(), visitDTO);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
} }

View File

@ -24,7 +24,6 @@ public class QrCodeController {
@PostMapping("/create") @PostMapping("/create")
@SecurityRequirement(name = "basicAuth") @SecurityRequirement(name = "basicAuth")
public ResponseEntity<QrCode> createQrCode(Authentication authentication, @RequestParam String name) { public ResponseEntity<QrCode> createQrCode(Authentication authentication, @RequestParam String name) {
Auth.getEmployee(authentication);
return ResponseEntity.ok(qrCodeService.createQrCode(name)); return ResponseEntity.ok(qrCodeService.createQrCode(name));
} }
} }

View File

@ -6,6 +6,7 @@ import com.example.onomatopoeiaback.service.VisitService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@ -22,6 +23,7 @@ public class VisitController {
@GetMapping("/{login}/visits") @GetMapping("/{login}/visits")
@SecurityRequirement(name = "basicAuth") @SecurityRequirement(name = "basicAuth")
public ResponseEntity<Page<Visit>> getVisits( public ResponseEntity<Page<Visit>> getVisits(
Authentication authentication,
@PathVariable String login, @PathVariable String login,
@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) { @RequestParam(defaultValue = "10") int size) {

View File

@ -2,7 +2,6 @@ package com.example.onomatopoeiaback.security;
import com.example.onomatopoeiaback.exceptions.BadRequestException; import com.example.onomatopoeiaback.exceptions.BadRequestException;
import com.example.onomatopoeiaback.exceptions.ForbiddenException; import com.example.onomatopoeiaback.exceptions.ForbiddenException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;

View File

@ -9,11 +9,13 @@ import org.springframework.security.web.AuthenticationEntryPoint;
import java.io.IOException; import java.io.IOException;
public class NoPopupBasicAuthenticationEntryPoint implements AuthenticationEntryPoint { public class NoPopupBasicAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override @Override
public void commence(HttpServletRequest request, HttpServletResponse response, public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException { AuthenticationException authException) throws IOException, ServletException {
response.sendError(HttpServletResponse.SC_FORBIDDEN, authException.getMessage()); int statusCode = response.getStatus();
if (statusCode == 200) {
statusCode = HttpServletResponse.SC_FORBIDDEN;
}
response.sendError(statusCode, authException.getMessage());
} }
} }

View File

@ -8,8 +8,11 @@ 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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.SecurityFilterChain;
@Configuration @Configuration

View File

@ -10,4 +10,4 @@ spring.datasource.password=MobileDev
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
# set up https # set up https
server.forward-headers-strategy=framework server.forward-headers-strategy=framework

View File