Я пробую демонстрационный проект с весенней загрузкой 2.1.1 и spring sec 5 в качестве сервера ресурсов OAuth2, однако, когда я пытаюсь запустить следующее
ENV
Ядро безопасности Spring 5.1.2
Java 8
КОД
@RestController
@SpringBootApplication
// @EnableResourceServer
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@GetMapping("/hello")
public String sayHello() {
return "Hello World";
}
@Configuration
static class MyWebSecurityConfigurerAdapter extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().authenticated()
.and()
.oauth2ResourceServer().jwt(); // <--- throws error
}
}
}
Что вызывает ошибку
Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/security/oauth2/server/resource/web/access/BearerTokenAccessDeniedHandler
СТРОИТЬ
Мои зависимости выглядят так
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-security')
implementation(group: 'org.springframework.security.oauth.boot', name: 'spring-security-oauth2-autoconfigure', version: '2.1.1.RELEASE')
implementation(group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.3.4.RELEASE')
}




Я тоже.
Но нашел в org.springframework.boot:spring-boot-starter-oauth2-resource-server
Кстати, я импортировал пакет org.springframework.boot:spring-boot-starter-oauth2-resource-server и использую:
http
.authorizeRequests()
.antMatchers("/**").hasAnyRole("admin")
.anyRequest().authenticated();
//.and()
//.oauth2ResourceServer() don't use it,
//.jwt();
// Do not use it, otherwise you must define
// jwtDecoderByIssuerUri or jwtDecoderByJwkKeySetUri
Если вы хотите включить oauth2ResourceServer, возможно, вам нужно дождаться Spring Security 5.3.x.
Может это связано с Поддержка следующего поколения-oauth-2-0 с пружинной безопасностью
Исключение исчезло, когда я добавил зависимость spring-boot-starter-oauth2-resource-server
Я столкнулся с той же проблемой и надеюсь, что найденное мной решение кому-то поможет.
Когда вы используете spring-security-oauth2 и включаете сервер ресурсов, используйте WebSecurityConfigurerAdapter для конфигураций безопасности конечной точки и переопределите метод public void configure(HttpSecurity security) throws Exception{}
Я поддержал другие ответы, но я использовал «реализацию» в моем файле build.gradle.
implementation 'org.springframework.security:spring-security-oauth2-resource-server'
The compile configuration still exists but should not be used as it will not offer the guarantees that the api and implementation configurations provide.
Приведенная выше цитата из: ((https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation))
Мой полный файл ниже
plugins {
id 'java'
}
group 'com.mycompany.mything'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
// implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.security:spring-security-oauth2-resource-server'
implementation 'org.springframework.security:spring-security-oauth2-jose'
// implementation 'org.springframework.security:spring-security-config'
}
Также см
https://medium.com/mindorks/implementation-vs-api-in-gradle-3-0-494c817a6fa
для реализации против обсуждения api