Spring boot 春季五号安全:';没有为id null';映射的密码编码器;
我正在尝试运行一个基本的安全应用程序,当我在Postman中输入凭据时,我没有收到预期的令牌。我正在遵循一个基本的教程,并且正确地遵循了所有的步骤,但是我得到了401“未授权”状态。我已经尝试使用Bcrypt密码编码器,我得到了相同的结果Spring boot 春季五号安全:';没有为id null';映射的密码编码器;,spring-boot,spring-security,oauth-2.0,Spring Boot,Spring Security,Oauth 2.0,我正在尝试运行一个基本的安全应用程序,当我在Postman中输入凭据时,我没有收到预期的令牌。我正在遵循一个基本的教程,并且正确地遵循了所有的步骤,但是我得到了401“未授权”状态。我已经尝试使用Bcrypt密码编码器,我得到了相同的结果 package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter{
@Autowired
private AuthenticationManager authManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception{
endpoints.authenticationManager(authManager);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient("webapp").secret("websecret").authorizedGrantTypes("password").scopes("read,write,trust");
}
}
注册类型为
BCryptPasswordEncoder
的bean,并在密码(“password1”)
和密码(“websecret”)
中使用该编码器
WebSecurityConfig.java
@配置
公共类WebSecurityConfig扩展了WebSecurityConfigureAdapter{
// ...
@凌驾
受保护的无效配置(AuthenticationManagerBuilder auth)引发异常{
auth.inMemoryAuthentication().withUser(“user1”)
.password(此.passwordEncoder().encode(“password1”)).roles(“用户”);
}
@豆子
@初级的
公共BCryptPasswordEncoder passwordEncoder(){
返回新的BCryptPasswordEncoder();
}
}
AuthorizationServerConfig.java
@配置
公共类AuthorizationServerConfig扩展AuthorizationServerConfigurerAdapter{
// ...
@自动连线
专用BCryptPasswordEncoder密码编码器;
// ...
@凌驾
公共无效配置(ClientDetailsServiceConfigurer客户端)引发异常{
clients.inMemory().withClient(“webapp”).secret(passwordEncoder.encode(“websecret”)).authorizedGrantTypes(“password”).scopes(“读、写、信任”);
}
}
可能与我的经历相同。好像他们甚至没有测试自己的演示
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public AuthenticationManager authenticationManager() throws Exception{
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password("password1").roles("USER");
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
@SpringBootApplication
@EnableAuthorizationServer
public class SpringMicroserviceOathApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMicroserviceOathApplication.class, args);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-microservice-oath</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-microservice-oath</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>