#11 -- Add spring security
This commit is contained in:
parent
056ffef87c
commit
26cf6d0b03
@ -24,11 +24,13 @@ dependencies {
|
|||||||
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
|
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-jetty'
|
||||||
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
|
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-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: 'org.json', name: 'json', version: '20220320'
|
||||||
|
|
||||||
implementation group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect'
|
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.module', name: 'jackson-module-afterburner'
|
||||||
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5'
|
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5'
|
||||||
implementation group: 'com.h2database', name:'h2'
|
implementation group: 'com.h2database', name:'h2'
|
||||||
|
@ -4,6 +4,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import ru.ulstu.fc.project.model.Project;
|
import ru.ulstu.fc.project.model.Project;
|
||||||
import ru.ulstu.fc.project.model.ProjectForm;
|
import ru.ulstu.fc.project.model.ProjectForm;
|
||||||
import ru.ulstu.fc.project.repository.ProjectRepository;
|
import ru.ulstu.fc.project.repository.ProjectRepository;
|
||||||
|
import ru.ulstu.fc.user.utils.UserUtils;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -45,6 +46,6 @@ public class ProjectService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isUserProject(Project project) {
|
private boolean isUserProject(Project project) {
|
||||||
return (userSevice.getCurrentUser().equals(project.getUser()));
|
return (UserUtils.getCurrentUserLogin().equals(project.getUser().getLogin()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package ru.ulstu.fc.user;
|
package ru.ulstu.fc.user.model;
|
||||||
|
|
||||||
public class UserNotFoundException extends RuntimeException {
|
public class UserNotFoundException extends RuntimeException {
|
||||||
public UserNotFoundException(String message) {
|
public UserNotFoundException(String message) {
|
@ -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.EntityGraph;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
@ -1,4 +1,4 @@
|
|||||||
package ru.ulstu.fc.user;
|
package ru.ulstu.fc.user.repository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import ru.ulstu.fc.user.model.UserRole;
|
import ru.ulstu.fc.user.model.UserRole;
|
@ -1,4 +1,4 @@
|
|||||||
package ru.ulstu.fc.user;
|
package ru.ulstu.fc.user.repository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import ru.ulstu.fc.user.model.UserSession;
|
import ru.ulstu.fc.user.model.UserSession;
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package ru.ulstu.fc.user;
|
package ru.ulstu.fc.user.service;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -10,8 +10,11 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import ru.ulstu.fc.user.model.User;
|
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.UserRole;
|
||||||
import ru.ulstu.fc.user.model.UserRoleConstants;
|
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.Collections;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -82,16 +85,4 @@ public class UserService implements UserDetailsService {
|
|||||||
public void initDefaultAdmin() {
|
public void initDefaultAdmin() {
|
||||||
createDefaultUser("admin", UserRoleConstants.ADMIN);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package ru.ulstu.fc.user;
|
package ru.ulstu.fc.user.service;
|
||||||
|
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
@ -1,4 +1,4 @@
|
|||||||
package ru.ulstu.fc.user;
|
package ru.ulstu.fc.user.service;
|
||||||
|
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
@ -1,11 +1,13 @@
|
|||||||
package ru.ulstu.fc.user;
|
package ru.ulstu.fc.user.service;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import ru.ulstu.fc.user.model.User;
|
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.model.UserSession;
|
||||||
|
import ru.ulstu.fc.user.repository.UserSessionRepository;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Transactional
|
@Transactional
|
@ -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.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContext;
|
import org.springframework.security.core.context.SecurityContext;
|
Loading…
x
Reference in New Issue
Block a user