#11 -- Add spring security

This commit is contained in:
Anton Romanov 2025-02-14 19:45:45 +04:00
parent 056ffef87c
commit 26cf6d0b03
12 changed files with 41 additions and 23 deletions

View File

@ -24,11 +24,13 @@ dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jetty'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-webflux'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security'
implementation group: 'org.json', name: 'json', version: '20220320'
implementation group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect'
implementation group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity6'
implementation group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner'
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5'
implementation group: 'com.h2database', name:'h2'

View File

@ -4,6 +4,7 @@ import org.springframework.stereotype.Service;
import ru.ulstu.fc.project.model.Project;
import ru.ulstu.fc.project.model.ProjectForm;
import ru.ulstu.fc.project.repository.ProjectRepository;
import ru.ulstu.fc.user.utils.UserUtils;
import java.util.List;
@ -45,6 +46,6 @@ public class ProjectService {
}
private boolean isUserProject(Project project) {
return (userSevice.getCurrentUser().equals(project.getUser()));
return (UserUtils.getCurrentUserLogin().equals(project.getUser().getLogin()));
}
}

View File

@ -1,4 +1,4 @@
package ru.ulstu.fc.user;
package ru.ulstu.fc.user.model;
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {

View File

@ -1,4 +1,4 @@
package ru.ulstu.fc.user;
package ru.ulstu.fc.user.repository;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;

View File

@ -1,4 +1,4 @@
package ru.ulstu.fc.user;
package ru.ulstu.fc.user.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.fc.user.model.UserRole;

View File

@ -1,4 +1,4 @@
package ru.ulstu.fc.user;
package ru.ulstu.fc.user.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.fc.user.model.UserSession;

View File

@ -0,0 +1,22 @@
package ru.ulstu.fc.user.service;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;
public final class IpAddressResolver {
private static final String CLIENT_IP_HEADER = "Client-IP";
private static final String FORWARDED_FOR_HEADER = "X-Forwarded-For";
public static String getRemoteAddr(HttpServletRequest request) {
String headerClientIp = request.getHeader("");
String headerXForwardedFor = request.getHeader(HttpServletRequest.FORM_AUTH);
if (StringUtils.isEmpty(request.getRemoteAddr()) && !StringUtils.isEmpty(headerClientIp)) {
return headerClientIp;
}
if (!StringUtils.isEmpty(headerXForwardedFor)) {
return headerXForwardedFor;
}
return request.getRemoteAddr();
}
}

View File

@ -1,4 +1,4 @@
package ru.ulstu.fc.user;
package ru.ulstu.fc.user.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -10,8 +10,11 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.fc.user.model.User;
import ru.ulstu.fc.user.model.UserNotFoundException;
import ru.ulstu.fc.user.model.UserRole;
import ru.ulstu.fc.user.model.UserRoleConstants;
import ru.ulstu.fc.user.repository.UserRepository;
import ru.ulstu.fc.user.repository.UserRoleRepository;
import java.util.Collections;
import java.util.Optional;
@ -82,16 +85,4 @@ public class UserService implements UserDetailsService {
public void initDefaultAdmin() {
createDefaultUser("admin", UserRoleConstants.ADMIN);
}
public void initDefaultAspirant() {
createDefaultUser("aspirant", UserRoleConstants.ASPIRANT);
}
public void initDefaultManager() {
createDefaultUser("manager", UserRoleConstants.MANAGER);
}
public void initDefaultHead() {
createDefaultUser("head", UserRoleConstants.HEAD);
}
}

View File

@ -1,4 +1,4 @@
package ru.ulstu.fc.user;
package ru.ulstu.fc.user.service;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;

View File

@ -1,4 +1,4 @@
package ru.ulstu.fc.user;
package ru.ulstu.fc.user.service;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;

View File

@ -1,11 +1,13 @@
package ru.ulstu.fc.user;
package ru.ulstu.fc.user.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.fc.user.model.User;
import ru.ulstu.fc.user.model.UserNotFoundException;
import ru.ulstu.fc.user.model.UserSession;
import ru.ulstu.fc.user.repository.UserSessionRepository;
@Service
@Transactional

View File

@ -1,4 +1,4 @@
package ru.ulstu.fc.user;
package ru.ulstu.fc.user.utils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;