MethodSecurity с CustomPermissionEvaluator не работает для SpringSecurity - PullRequest
0 голосов
/ 30 августа 2018

Я борюсь со своей конфигурацией Spring Security, которую я до сих пор не смог заставить работать. нужна твоя помощь.

Это моя конфигурация:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {

    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler() {
        DefaultMethodSecurityExpressionHandler expressionHandler =
                new DefaultMethodSecurityExpressionHandler();
        expressionHandler.setPermissionEvaluator(new CustomPermissionEvaluator());
        return expressionHandler;
    }
}

Это CustomPermissionEvaluator:

public class CustomPermissionEvaluator implements PermissionEvaluator {

    @Override
    public boolean hasPermission(Authentication auth, Object targetDomainObject, Object permission) {
        return true;
    }

    @Override
    public boolean hasPermission(Authentication auth, Serializable targetId, String targetType, Object permission) {
        return true;
    }
}

Это контроллер:

@PreAuthorize("hasPermission(#test, 'write')")
@PostMapping("/test")
public Result saveTest(@RequestBody Test test) {
    return Result.build();
}

Все выглядит хорошо и просто, но когда я запрашиваю остальные API, получаю следующее исключение:

.m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext

Кто-нибудь может дать мне подсказку, что делать? Благодаря ~

1 Ответ

0 голосов
/ 03 сентября 2018

Ошибка указывает на отсутствие объекта Authentication вместо Authorization. Вы определили UserDetailsService и уверены, что ваша аутентификация прошла успешно?

В этом блоге вы можете подключить свой userDetailService, определив метод для получения AuthenticationManagerBuilder. В этом компоновщике вы можете либо определить простой inMemoryAuthentication для тестирования, либо связать свою реализацию AuthenticationProvider или UserDetailService.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...