display user in filter
This commit is contained in:
parent
da7a17ee49
commit
48894b2176
@ -58,7 +58,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
|||||||
.anyRequest()
|
.anyRequest()
|
||||||
.permitAll();
|
.permitAll();
|
||||||
http.anonymous()
|
http.anonymous()
|
||||||
.principal("developer")
|
.principal("admin")
|
||||||
.authorities(UserRoleConstants.ADMIN);
|
.authorities(UserRoleConstants.ADMIN);
|
||||||
} else {
|
} else {
|
||||||
log.debug("Security enabled");
|
log.debug("Security enabled");
|
||||||
|
@ -6,7 +6,7 @@ import org.springframework.validation.FieldError;
|
|||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import ru.ulstu.core.error.EntityIdIsNullException;
|
import ru.ulstu.core.error.EntityIdIsNullException;
|
||||||
import ru.ulstu.core.model.ErrorConstants;
|
import ru.ulstu.core.model.ErrorConstants;
|
||||||
import ru.ulstu.core.model.response.Response;
|
import ru.ulstu.core.model.response.Response;
|
||||||
@ -20,33 +20,29 @@ import ru.ulstu.user.error.UserNotActivatedException;
|
|||||||
import ru.ulstu.user.error.UserNotFoundException;
|
import ru.ulstu.user.error.UserNotFoundException;
|
||||||
import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException;
|
import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException;
|
||||||
import ru.ulstu.user.error.UserResetKeyError;
|
import ru.ulstu.user.error.UserResetKeyError;
|
||||||
|
import ru.ulstu.user.service.UserService;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import org.springframework.ui.Model;
|
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
||||||
import ru.ulstu.user.service.UserService;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
public class AdviceController {
|
public class AdviceController {
|
||||||
private final static String USER_NAME_TEMPLATE = "%s %s %s";
|
private final static String USER_NAME_TEMPLATE = "%s %s %s";
|
||||||
private final Logger log = LoggerFactory.getLogger(AdviceController.class);
|
private final Logger log = LoggerFactory.getLogger(AdviceController.class);
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
|
|
||||||
public AdviceController(UserService userService) {
|
public AdviceController(UserService userService) {
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ModelAttribute
|
@ModelAttribute("currentUser")
|
||||||
public void globalAttributes(Model model) {
|
public String getCurrentUser() {
|
||||||
model.addAttribute("currentUser", String.format(USER_NAME_TEMPLATE,
|
return String.format(USER_NAME_TEMPLATE,
|
||||||
userService.getCurrentUser().getLastName(),
|
userService.getCurrentUser().getLastName(),
|
||||||
userService.getCurrentUser().getFirstName().substring(0, 1),
|
userService.getCurrentUser().getFirstName().substring(0, 1),
|
||||||
userService.getCurrentUser().getFirstName().substring(0, 1)));
|
userService.getCurrentUser().getFirstName().substring(0, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Response<Void> handleException(ErrorConstants error) {
|
private Response<Void> handleException(ErrorConstants error) {
|
||||||
log.warn(error.toString());
|
log.warn(error.toString());
|
||||||
return new Response<>(error);
|
return new Response<>(error);
|
||||||
|
21
src/main/java/ru/ulstu/index/controller/IndexController.java
Normal file
21
src/main/java/ru/ulstu/index/controller/IndexController.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package ru.ulstu.index.controller;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.ModelMap;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import ru.ulstu.core.controller.AdviceController;
|
||||||
|
import ru.ulstu.user.service.UserService;
|
||||||
|
|
||||||
|
@Controller()
|
||||||
|
@RequestMapping(value = "/index")
|
||||||
|
public class IndexController extends AdviceController {
|
||||||
|
public IndexController(UserService userService) {
|
||||||
|
super(userService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public void currentUser(ModelMap modelMap) {
|
||||||
|
//нужен здесь для добавления параметров на стартовой странице
|
||||||
|
}
|
||||||
|
}
|
@ -315,7 +315,7 @@ public class UserService implements UserDetailsService {
|
|||||||
public User findById(Integer id) {
|
public User findById(Integer id) {
|
||||||
return userRepository.findOne(id);
|
return userRepository.findOne(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getCurrentUser() {
|
public User getCurrentUser() {
|
||||||
String login = UserUtils.getCurrentUserLogin();
|
String login = UserUtils.getCurrentUserLogin();
|
||||||
User user = userRepository.findOneByLoginIgnoreCase(login);
|
User user = userRepository.findOneByLoginIgnoreCase(login);
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
package ru.ulstu.user.service;
|
|
||||||
import org.apache.commons.lang3.RandomStringUtils;
|
|
||||||
import org.springframework.security.core.Authentication;
|
|
||||||
import org.springframework.security.core.context.SecurityContext;
|
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
|
||||||
|
|
||||||
public class UserUtils {
|
|
||||||
private static final int DEF_COUNT = 20;
|
|
||||||
|
|
||||||
public static String generateActivationKey() {
|
|
||||||
return RandomStringUtils.randomNumeric(DEF_COUNT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String generateResetKey() {
|
|
||||||
return RandomStringUtils.randomNumeric(DEF_COUNT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getCurrentUserLogin() {
|
|
||||||
final SecurityContext securityContext = SecurityContextHolder.getContext();
|
|
||||||
if (securityContext == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final Authentication authentication = securityContext.getAuthentication();
|
|
||||||
if (authentication.getPrincipal() instanceof UserDetails) {
|
|
||||||
final UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
|
|
||||||
return springSecurityUser.getUsername();
|
|
||||||
}
|
|
||||||
if (authentication.getPrincipal() instanceof String) {
|
|
||||||
return (String) authentication.getPrincipal();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="ru"
|
<html lang="ru"
|
||||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||||
<head>
|
<head>
|
||||||
|
|
||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
|
Loading…
Reference in New Issue
Block a user