update deps, fix null pointer

This commit is contained in:
_Bastler 2022-11-30 15:17:30 +01:00
parent 3586bdbbcf
commit 74394f0582
8 changed files with 199 additions and 51 deletions

View File

@ -9,6 +9,7 @@ import java.time.format.DateTimeFormatter;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@ -46,9 +47,22 @@ public class PermissionManager implements UserDataProvider {
* @return the list
*/
public List<Permission> get(Long target, String name) {
return get(target, name, null);
}
/**
* Gets the.
*
* @param target the target
* @param name the name
* @param sort the sort
* @return the list
*/
public List<Permission> get(Long target, String name, String sort) {
if (target != null && StringUtils.hasText(name)) {
return Lists.newArrayList(
permissionRepository.findAll(qPermission.target.eq(target).and(qPermission.name.eq(name))));
permissionRepository.findAll(qPermission.target.eq(target).and(qPermission.name.eq(name)),
Sort.by(StringUtils.hasText(sort) ? sort : "id")));
}
return Lists.newArrayList();
}
@ -61,10 +75,27 @@ public class PermissionManager implements UserDataProvider {
* @return the not expires
*/
public List<Permission> getNotExpires(Long target, String name) {
return getNotExpires(target, name, null);
}
/**
* Gets the not expires.
*
* @param target the target
* @param name the name
* @param sort the sort
* @return the not expires
*/
public List<Permission> getNotExpires(Long target, String name, String sort) {
if (target != null && StringUtils.hasText(name)) {
return Lists.newArrayList(permissionRepository.findAll(qPermission.target.eq(target)
.and(qPermission.name.eq(name)).and(qPermission.expires.after(Instant.now())
.and(qPermission.starts.isNull().or(qPermission.starts.before(Instant.now()))))));
return Lists
.newArrayList(
permissionRepository.findAll(
qPermission.target.eq(target).and(qPermission.name.eq(name))
.and(qPermission.expires.after(Instant.now())
.and(qPermission.starts.isNull()
.or(qPermission.starts.before(Instant.now())))),
Sort.by(StringUtils.hasText(sort) ? sort : "id")));
}
return Lists.newArrayList();
}
@ -76,8 +107,20 @@ public class PermissionManager implements UserDataProvider {
* @return the all by target
*/
public List<Permission> getAllByTarget(Long target) {
return getAllByTarget(target, null);
}
/**
* Gets the all by target.
*
* @param target the target
* @param sort the sort
* @return the all by target
*/
public List<Permission> getAllByTarget(Long target, String sort) {
if (target != null) {
return Lists.newArrayList(permissionRepository.findAll(qPermission.target.eq(target)));
return Lists.newArrayList(permissionRepository.findAll(qPermission.target.eq(target),
Sort.by(StringUtils.hasText(sort) ? sort : "id")));
}
return Lists.newArrayList();
}
@ -89,10 +132,26 @@ public class PermissionManager implements UserDataProvider {
* @return the not expires by target
*/
public List<Permission> getNotExpiresByTarget(Long target) {
return getNotExpiresByTarget(target, null);
}
/**
* Gets the not expires by target.
*
* @param target the target
* @param sort the sort
* @return the not expires by target
*/
public List<Permission> getNotExpiresByTarget(Long target, String sort) {
if (target != null) {
return Lists.newArrayList(permissionRepository
.findAll(qPermission.target.eq(target).and(qPermission.expires.after(Instant.now())
.and(qPermission.starts.isNull().or(qPermission.starts.before(Instant.now()))))));
return Lists
.newArrayList(
permissionRepository.findAll(
qPermission.target.eq(target)
.and(qPermission.expires.after(Instant.now())
.and(qPermission.starts.isNull()
.or(qPermission.starts.before(Instant.now())))),
Sort.by(StringUtils.hasText(sort) ? sort : "id")));
}
return Lists.newArrayList();
}
@ -104,10 +163,26 @@ public class PermissionManager implements UserDataProvider {
* @return the not expires by name
*/
public List<Permission> getNotExpiresByName(String name) {
return getNotExpiresByName(name, null);
}
/**
* Gets the not expires by name.
*
* @param name the name
* @param sort the sort
* @return the not expires by name
*/
public List<Permission> getNotExpiresByName(String name, String sort) {
if (name != null) {
return Lists.newArrayList(
permissionRepository.findAll(qPermission.name.eq(name).and(qPermission.expires.after(Instant.now())
.and(qPermission.starts.isNull().or(qPermission.starts.before(Instant.now()))))));
return Lists
.newArrayList(
permissionRepository.findAll(
qPermission.name.eq(name)
.and(qPermission.expires.after(Instant.now())
.and(qPermission.starts.isNull()
.or(qPermission.starts.before(Instant.now())))),
Sort.by(StringUtils.hasText(sort) ? sort : "id")));
}
return Lists.newArrayList();
}
@ -119,9 +194,21 @@ public class PermissionManager implements UserDataProvider {
* @return the not expires by target ignore start
*/
public List<Permission> getNotExpiresByTargetIgnoreStart(Long target) {
return getNotExpiresByTargetIgnoreStart(target, null);
}
/**
* Gets the not expires by target ignore start.
*
* @param target the target
* @param sort the sort
* @return the not expires by target ignore start
*/
public List<Permission> getNotExpiresByTargetIgnoreStart(Long target, String sort) {
if (target != null) {
return Lists.newArrayList(permissionRepository
.findAll(qPermission.target.eq(target).and(qPermission.expires.after(Instant.now()))));
return Lists.newArrayList(permissionRepository.findAll(
qPermission.target.eq(target).and(qPermission.expires.after(Instant.now())),
Sort.by(StringUtils.hasText(sort) ? sort : "id")));
}
return Lists.newArrayList();
}
@ -185,7 +272,7 @@ public class PermissionManager implements UserDataProvider {
public Permission update(Permission permission) {
Assert.isTrue(permissionRepository.existsById(permission.getId()),
"Permission '" + permission.getName() + "' for target + '" + permission.getTarget() + "' not exists!");
Permission updatePermission = permissionRepository.getById(permission.getId());
Permission updatePermission = permissionRepository.findById(permission.getId()).orElse(new Permission());
updatePermission.setStarts(permission.getStarts());
updatePermission.setExpires(permission.getExpires());
updatePermission.setAddon(permission.isAddon());

View File

@ -6,8 +6,10 @@ package de.bstly.we.businesslogic;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.google.common.collect.Lists;
@ -50,7 +52,19 @@ public class QuotaManager implements UserDataProvider {
* @return the all by name
*/
public List<Quota> getAllByName(String name) {
return Lists.newArrayList(quotaRepository.findAll(qQuota.name.eq(name)));
return getAllByName(name, null);
}
/**
* Gets the all by name.
*
* @param name the name
* @param sort the sort
* @return the all by name
*/
public List<Quota> getAllByName(String name, String sort) {
return Lists.newArrayList(
quotaRepository.findAll(qQuota.name.eq(name), Sort.by(StringUtils.hasText(sort) ? sort : "id")));
}
/**
@ -60,8 +74,20 @@ public class QuotaManager implements UserDataProvider {
* @return the all by target
*/
public List<Quota> getAllByTarget(Long target) {
return getAllByTarget(target, null);
}
/**
* Gets the all by target.
*
* @param target the target
* @param sort the sort
* @return the all by target
*/
public List<Quota> getAllByTarget(Long target, String sort) {
if (target != null) {
return Lists.newArrayList(quotaRepository.findAll(qQuota.target.eq(target)));
return Lists.newArrayList(quotaRepository.findAll(qQuota.target.eq(target),
Sort.by(StringUtils.hasText(sort) ? sort : "id")));
}
return Lists.newArrayList();
}
@ -73,8 +99,20 @@ public class QuotaManager implements UserDataProvider {
* @return the not expires by target
*/
public List<Quota> getNotExpiresByTarget(Long target) {
return getNotExpiresByTarget(target, null);
}
/**
* Gets the not expires by target.
*
* @param target the target
* @param sort the sort
* @return the not expires by target
*/
public List<Quota> getNotExpiresByTarget(Long target, String sort) {
if (target != null) {
return Lists.newArrayList(quotaRepository.findAll(qQuota.target.eq(target).and(qQuota.value.gt(0))));
return Lists.newArrayList(quotaRepository.findAll(qQuota.target.eq(target).and(qQuota.value.gt(0)),
Sort.by(StringUtils.hasText(sort) ? sort : "id")));
}
return Lists.newArrayList();
}

View File

@ -4,6 +4,7 @@
package de.bstly.we.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists;
@ -41,36 +43,40 @@ public class PermissionManagementController extends BaseController {
* Gets the permissions for user.
*
* @param username the username
* @param sort the sort
* @return the permissions for user
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping("/{username}")
public List<Permission> getPermissionsForUser(@PathVariable("username") String username) {
public List<Permission> getPermissionsForUser(@PathVariable("username") String username,
@RequestParam("sort") Optional<String> sort) {
User user = userManager.getByUsername(username);
if (user == null) {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
return permissionManager.getNotExpiresByTargetIgnoreStart(user.getId());
return permissionManager.getNotExpiresByTargetIgnoreStart(user.getId(), sort.orElse(null));
}
/**
* Gets the all permissions for user.
*
* @param username the username
* @param sort the sort
* @return the all permissions for user
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping("/{username}/all")
public List<Permission> getAllPermissionsForUser(@PathVariable("username") String username) {
public List<Permission> getAllPermissionsForUser(@PathVariable("username") String username,
@RequestParam("sort") Optional<String> sort) {
User user = userManager.getByUsername(username);
if (user == null) {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
return permissionManager.getAllByTarget(user.getId());
return permissionManager.getAllByTarget(user.getId(), sort.orElse(null));
}
/**

View File

@ -4,6 +4,7 @@
package de.bstly.we.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.beust.jcommander.internal.Lists;
@ -41,48 +43,53 @@ public class QuotaManagementController extends BaseController {
* Gets the quotas for user.
*
* @param username the username
* @param sort the sort
* @return the quotas for user
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping("/{username}")
public List<Quota> getQuotasForUser(@PathVariable("username") String username) {
public List<Quota> getQuotasForUser(@PathVariable("username") String username,
@RequestParam("sort") Optional<String> sort) {
User user = userManager.getByUsername(username);
if (user == null) {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
return quotaManager.getNotExpiresByTarget(user.getId());
return quotaManager.getNotExpiresByTarget(user.getId(), sort.orElse(null));
}
/**
* Gets the all quotas for user.
*
* @param username the username
* @param sort the sort
* @return the all quotas for user
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping("/{username}/all")
public List<Quota> getAllQuotasForUser(@PathVariable("username") String username) {
public List<Quota> getAllQuotasForUser(@PathVariable("username") String username,
@RequestParam("sort") Optional<String> sort) {
User user = userManager.getByUsername(username);
if (user == null) {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
return quotaManager.getAllByTarget(user.getId());
return quotaManager.getAllByTarget(user.getId(), sort.orElse(null));
}
/**
* Gets the quotas by name.
*
* @param name the name
* @param sort the sort
* @return the quotas by name
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping("/byname/{name}")
public List<Quota> getQuotasByName(@PathVariable("name") String name) {
return quotaManager.getAllByName(name);
public List<Quota> getQuotasByName(@PathVariable("name") String name, @RequestParam("sort") Optional<String> sort) {
return quotaManager.getAllByName(name, sort.orElse(null));
}
/**

View File

@ -74,13 +74,17 @@ public class UserManagementController extends BaseController {
*
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param sort the sort
* @param descending the descending
* @return the users
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping
public Page<User> getUsers(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter) {
return userManager.get(pageParameter.orElse(0), sizeParameter.orElse(10), "username", true);
@RequestParam("size") Optional<Integer> sizeParameter, @RequestParam("sort") Optional<String> sort,
@RequestParam("descending") Optional<Boolean> descending) {
return userManager.get(pageParameter.orElse(0), sizeParameter.orElse(10), sort.orElse("username"),
descending.orElse(false));
}
/**

View File

@ -11,13 +11,14 @@ import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@ -48,7 +49,7 @@ import dev.samstevens.totp.code.HashingAlgorithm;
*/
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
public class SecurityConfig {
@Autowired
private LocalUserDetailsService localUserDetailsService;
@ -92,13 +93,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
auth.authenticationProvider(localAuthenticationProvider);
}
/*
* @see org.springframework.security.config.annotation.web.configuration.
* WebSecurityConfigurerAdapter#configure(org.springframework.security.config.
* annotation.web.builders.HttpSecurity)
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
// anonymous
.anonymous().authenticationFilter(localAnonymousAuthenticationFilter()).and()
@ -115,11 +111,11 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
// remember me
.rememberMe().rememberMeServices(rememberMeServices()).and()
// form totp
.addFilterBefore(formSecondFactorAuthenticationFilter(), LocalAnonymousAuthenticationFilter.class)
.addFilterBefore(formSecondFactorAuthenticationFilter(http), LocalAnonymousAuthenticationFilter.class)
// rest login
.addFilterBefore(restAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(restAuthenticationFilter(http), UsernamePasswordAuthenticationFilter.class)
// rest totp
.addFilterAfter(restSecondFactorAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.addFilterAfter(restSecondFactorAuthenticationFilter(http), UsernamePasswordAuthenticationFilter.class)
// Logout
.logout().logoutUrl("/auth/logout").logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
.and()
@ -137,6 +133,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
// cors
http.cors().configurationSource(corsConfigurationSource());
}
return http.build();
}
/**
@ -206,6 +204,12 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
return restAuthenticationSuccessHandler;
}
@Bean
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
return http.getSharedObject(AuthenticationManagerBuilder.class).userDetailsService(localUserDetailsService)
.passwordEncoder(passwordEncoder).and().build();
}
/**
* Form second factor authentication filter.
*
@ -213,10 +217,11 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
* @throws Exception the exception
*/
@Bean
public FormSecondFactorAuthenticationFilter formSecondFactorAuthenticationFilter() throws Exception {
public FormSecondFactorAuthenticationFilter formSecondFactorAuthenticationFilter(HttpSecurity http)
throws Exception {
FormSecondFactorAuthenticationFilter formSecondFactorAuthenticationFilter = new FormSecondFactorAuthenticationFilter(
"/auth/login/2fa");
formSecondFactorAuthenticationFilter.setAuthenticationManager(authenticationManager());
formSecondFactorAuthenticationFilter.setAuthenticationManager(authenticationManager(http));
formSecondFactorAuthenticationFilter.setAuthenticationSuccessHandler(formAuthenticationSuccessHandler());
formSecondFactorAuthenticationFilter.setRememberMeServices(rememberMeServices());
return formSecondFactorAuthenticationFilter;
@ -229,9 +234,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
* @throws Exception the exception
*/
@Bean
public RestAuthenticationFilter restAuthenticationFilter() throws Exception {
public RestAuthenticationFilter restAuthenticationFilter(HttpSecurity http) throws Exception {
RestAuthenticationFilter restAuthenticationFilter = new RestAuthenticationFilter("/auth/restlogin");
restAuthenticationFilter.setAuthenticationManager(authenticationManager());
restAuthenticationFilter.setAuthenticationManager(authenticationManager(http));
restAuthenticationFilter.setAuthenticationSuccessHandler(restAuthenticationSuccessHandler());
restAuthenticationFilter.setAuthenticationFailureHandler(restAuthenticationFailureHandler);
return restAuthenticationFilter;
@ -244,10 +249,11 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
* @throws Exception the exception
*/
@Bean
public RestSecondFactorAuthenticationFilter restSecondFactorAuthenticationFilter() throws Exception {
public RestSecondFactorAuthenticationFilter restSecondFactorAuthenticationFilter(HttpSecurity http)
throws Exception {
RestSecondFactorAuthenticationFilter restSecondFactorAuthenticationFilter = new RestSecondFactorAuthenticationFilter(
"/auth/restlogin/2fa");
restSecondFactorAuthenticationFilter.setAuthenticationManager(authenticationManager());
restSecondFactorAuthenticationFilter.setAuthenticationManager(authenticationManager(http));
restSecondFactorAuthenticationFilter.setAuthenticationSuccessHandler(restAuthenticationSuccessHandler());
restSecondFactorAuthenticationFilter.setAuthenticationFailureHandler(restAuthenticationFailureHandler);
restSecondFactorAuthenticationFilter.setRememberMeServices(rememberMeServices());

View File

@ -378,7 +378,7 @@ public class OidcTokenManager implements SmartInitializingSingleton {
do {
page = oidcTokenRepository.findAll(predicate.getValue(), pageable);
for (OidcToken oidcToken : page.getContent()) {
if (oidcToken.getCreated()
if (oidcToken.getExpiresIn() != null && oidcToken.getCreated()
.isBefore(Instant.now().minus(oidcToken.getExpiresIn(), ChronoUnit.SECONDS))) {
logger.debug(
"delete expired OidcToken: " + oidcToken.getId() + " [" + oidcToken.getAccessToken() + "]");

View File

@ -12,14 +12,14 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>
<log4j2.version>2.17.2</log4j2.version>
<revision>1.9.2-SNAPSHOT</revision>
<log4j2.version>2.19.0</log4j2.version>
<revision>2.0.0-SNAPSHOT</revision>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<version>2.7.6</version>
<relativePath />
</parent>