我试图在我的API中向一个endpoint发出请求,然而,每次试图访问执行器endpoint都会返回401错误,访问这个资源需要完全身份验证。我只是使用spring security对用户的密码进行加密,然后再将它们存储到数据库中。我已经检查了所有类似的问题,并执行了他们给出的所有解决方案,但仍然没有运气。
server.port=8080
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/DevUserManagementDB
spring.datasource.username=root
spring.datasource.password=toor
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
management.security.enabled=false
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.2.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
securityconfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/users").permitAll()
.antMatchers("/motion").permitAll().and().csrf().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
userService.java
@Service
@Transactional
public class UserService implements UserDetailsService {
@Autowired
private PasswordEncoder passwordEncoder;
public Response AddUser(Users user) {
if( user == null ){
throw new ResourceNotFoundException("Empty", "Missing Data Exception");
} else {
user.setPassword(passwordEncoder.encode(user.getPassword()));
userRepository.save(user);
String customerNum = Long.toString(userDetails.getCustomerNumber);
return new Response("customerNum", "Customer Created Successfully");
}
}
}
如果您只使用Spring Security类,那么最简单的方法是通过删除@enableWebSecurity
来禁用Spring Security筛选器,即:
@Configuration
public class SecurityConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
如果你不想那样,你也可以试试这个:
http.authorizeRequests().antMatchers("/**").permitAll().and().csrf().disable();
我创建了一个SpringBoot/SpringSecurity/React web应用程序,在本地运行良好,但当我尝试在DigitalOcean生产服务器上运行它时,它失败了 未经授权的错误:访问此资源需要完全身份验证 我没有使用oauth2,只是Spring安全。主页不需要身份验证,所以我不明白错误。建议?
我正在应用程序中实现和进行身份验证。 我有3个角色:管理员,版主和用户。 例如,在使用user role登录之后,我得到了主页,但是当我通过点击一个按钮访问用户空间时,我得到了: 2020-09-04 09:01:22.819错误10148---[nio-8080-exec-5]C.B.S.Security.jwt.AuthentryPointJwt:未经授权的错误:访问此资源需要完全身份验证 文
application.properties代码 配置 日志 转到URLhttp://localhost:8088/swagger-用户界面/索引。html 如何修复?
当我试图使用访问令牌访问资源时,我正在使用https://github.com/Angel-git/wso2is-springoauth后的wso2is服务器进行Spring Boot安全性示例 {“error”:“未经授权”,“error_description”:“访问此资源需要完全身份验证”} 我正在通过以下方式生成访问令牌: curl-u client_id:client_secret-k
我正在测试Spring Security中的登录方法。我想获得200的地位,但来了401。