/
ZufarFaiz
/
SberProject
Обзор
Документация
Войти
/
ZufarFaiz
/
SberProject
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/main/java/com/example/JobExchange_app/controller/AuthController.java
89 строк
3 KB
Семейный
Вынес некоторые методы в отдельные контроллеры
01 июн 2025, 23:26
01 июн 2025, 23:26
aafad78
Код
Авторство
О чём код?
package com.example.JobExchange_app.controller; import com.example.JobExchange_app.dto.UserDto; import com.example.JobExchange_app.model.Role; import com.example.JobExchange_app.service.AuthService; import com.example.JobExchange_app.service.CompanyService; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; @Controller @RequestMapping("/auth") public class AuthController { @Autowired private AuthService authServiceImpl; @Autowired CompanyService companyService; private final AuthenticationManager authenticationManager; private final UserDetailsService userDetailsService; @Autowired public AuthController(AuthenticationManager authenticationManager, UserDetailsService userDetailsService) { this.authenticationManager = authenticationManager; this.userDetailsService = userDetailsService; } @GetMapping("/register") public String showRegistrationForm(Model model) { model.addAttribute("user", new UserDto()); model.addAttribute("roles", Role.getRegistrationRoles()); model.addAttribute("companies",companyService.getAllCompanies()); return "register"; } @PostMapping("/register") public String registerUser(@ModelAttribute("user") UserDto registrationDto, BindingResult result, Model model) { System.out.println("Company ID в контроллере: " + registrationDto.getCompanyId()); try { authServiceImpl.registerUser(registrationDto); return "redirect:/auth/home"; } catch (IllegalArgumentException e) { model.addAttribute("error", e.getMessage()); model.addAttribute("roles", Role.getRegistrationRoles()); return "register"; } } @GetMapping("/login") public String showLoginForm() { return "login"; } @PostMapping("/login") public String login(@RequestParam String username, @RequestParam String password, Model model, HttpServletRequest request) { try { Authentication authentication = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(username, password) ); SecurityContextHolder.getContext().setAuthentication(authentication); return null; } catch (BadCredentialsException e) { model.addAttribute("error", "Invalid username or password"); return "login"; } } }