diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 0bbe3c7..d70691a 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,11 +4,13 @@
-
+
+
+
+
-
@@ -49,6 +51,11 @@
"settings.editor.selected.configurable": "MavenSettings"
}
}]]>
+
+
+
+
+
@@ -129,7 +136,15 @@
1739967506973
-
+
+
+ 1739968326971
+
+
+
+ 1739968326971
+
+
@@ -138,17 +153,12 @@
-
+
+
-
- file://$PROJECT_DIR$/src/main/java/com/example/nto/entity/Code.java
- 20
-
-
-
file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java
29
@@ -160,9 +170,44 @@
- file://$PROJECT_DIR$/src/main/java/com/example/nto/websecurity/WebSecurityConfig.java
- 39
-
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java
+ 49
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java
+ 57
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java
+ 50
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java
+ 52
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java
+ 53
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java
+ 54
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java
+ 55
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java
+ 56
+
diff --git a/src/main/java/com/example/nto/controller/AuthController.java b/src/main/java/com/example/nto/controller/AuthController.java
index 5507420..313c3b1 100644
--- a/src/main/java/com/example/nto/controller/AuthController.java
+++ b/src/main/java/com/example/nto/controller/AuthController.java
@@ -1,10 +1,63 @@
package com.example.nto.controller;
-import org.springframework.web.bind.annotation.RestController;
+import lombok.AllArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.provisioning.InMemoryUserDetailsManager;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
@RestController
public class AuthController {
-// @PostMapping("/api/login/")
-// public ResponseEntity> login() {}
+ @AllArgsConstructor
+ private static class LoginBody {
+ private String login;
+ private String password;
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public String getLogin() {
+ return login;
+ }
+
+ public void setLogin(String login) {
+ this.login = login;
+ }
+ }
+
+
+ @Autowired
+ private AuthenticationManager authenticationManager;
+
+ /*
+ Эндпоинт авторизации, кинь сюда логин и пароль, и возможно я дам тебе возможность авторизоваться.
+ */
+ @PostMapping("/api/login/")
+ private ResponseEntity login(HttpServletRequest request, @RequestBody LoginBody loginBody) { //, @RequestParam String login, @RequestParam String password) {
+ UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
+ loginBody.getLogin(), loginBody.getPassword());
+ Authentication authentication = authenticationManager.authenticate(authRequest);
+ SecurityContext securityContext = SecurityContextHolder.getContext();
+ securityContext.setAuthentication(authentication);
+ HttpSession session = request.getSession(true);
+ session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
+ return ResponseEntity.status(HttpStatus.OK).build();
+ }
}
+
diff --git a/src/main/java/com/example/nto/entity/Code.java b/src/main/java/com/example/nto/entity/Code.java
index f371449..db60a73 100644
--- a/src/main/java/com/example/nto/entity/Code.java
+++ b/src/main/java/com/example/nto/entity/Code.java
@@ -5,16 +5,14 @@ import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
-import javax.persistence.Entity;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
+import javax.persistence.*;
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
+@Table(name = "code")
public class Code {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
diff --git a/src/main/java/com/example/nto/entity/Employee.java b/src/main/java/com/example/nto/entity/Employee.java
index a3ea427..9b1158c 100644
--- a/src/main/java/com/example/nto/entity/Employee.java
+++ b/src/main/java/com/example/nto/entity/Employee.java
@@ -18,6 +18,7 @@ import java.util.List;
@Builder
@NoArgsConstructor
@AllArgsConstructor
+@Table(name = "employee")
public class Employee {
@Id
diff --git a/src/main/java/com/example/nto/service/impl/UserDetailsServiceImpl.java b/src/main/java/com/example/nto/service/impl/UserDetailsServiceImpl.java
new file mode 100644
index 0000000..5479177
--- /dev/null
+++ b/src/main/java/com/example/nto/service/impl/UserDetailsServiceImpl.java
@@ -0,0 +1,22 @@
+package com.example.nto.service.impl;
+
+import com.example.nto.entity.Employee;
+import com.example.nto.repository.EmployeeRepository;
+import com.example.nto.websecurity.CustomUserDetails;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+
+public class UserDetailsServiceImpl implements UserDetailsService {
+
+
+ @Autowired
+ private EmployeeRepository repository;
+
+ @Override
+ public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
+ Employee employee = repository.findByLogin(s).orElseThrow(() -> new UsernameNotFoundException(s));
+ return new CustomUserDetails(employee);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/nto/websecurity/WebSecurityConfig.java b/src/main/java/com/example/nto/websecurity/WebSecurityConfig.java
index b177935..f088fe9 100644
--- a/src/main/java/com/example/nto/websecurity/WebSecurityConfig.java
+++ b/src/main/java/com/example/nto/websecurity/WebSecurityConfig.java
@@ -3,8 +3,11 @@ package com.example.nto.websecurity;
import com.example.nto.entity.Employee;
import com.example.nto.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitialization;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.DependsOn;
+import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -25,7 +28,14 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private EmployeeRepository employeeRepository;
+ @Override
@Bean
+ public AuthenticationManager authenticationManagerBean() throws Exception {
+ return super.authenticationManagerBean();
+ }
+
+ @Bean
+ @DependsOnDatabaseInitialization
public UserDetailsService userDetailsService() {
List employees = employeeRepository.findAll();
InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
@@ -64,7 +74,10 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
- http.authorizeRequests()
+ http
+ .csrf().disable()
+ .authorizeRequests()
+ .antMatchers("/api/login/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll()