【问题标题】:Error Unauthorized, Full authentication is required to access this resource错误未经授权,需要完全身份验证才能访问此资源
【发布时间】:2018-06-29 11:07:00
【问题描述】:

我正在尝试向我的 API 中的端点发出请求,但是每次尝试甚至访问执行器端点都会返回 401 错误需要完全身份验证才能访问此资源。我只使用 spring security 来加密我的用户密码,然后再将它们存储在数据库中。 我检查了所有类似的问题并实施了他们给出的所有解决方案,但仍然没有运气。

这是我的代码:

application.properties

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

pom.xml

<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");
      }

  }
}

【问题讨论】:

  • I am only using spring security for encrypting my users's passwords before storing them in the database你在哪里使用它?
  • 我用加密密码的类更新了问题

标签: java tomcat spring-boot spring-security spring-boot-actuator


【解决方案1】:

如果你只使用spring security classes,那么最简单的方法是通过删除@EnableWebSecurity来禁用spring security filter,即:

@Configuration
public class SecurityConfig  {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

如果你不想这样,你也可以试试这个:

http.authorizeRequests().antMatchers("/**").permitAll().and().csrf().disable();

【讨论】:

  • 即使我请求 localhost:8080 它也会返回“error”:“Unauthorized”,“message”:“Bad credentials”,
  • 这可能是Tomcat服务器问题
  • 你是作为war部署还是直接运行?
  • 我直接从 netbeans 运行它
  • 干净构建怎么样?
猜你喜欢
  • 2020-12-23
  • 2020-09-02
  • 2022-07-22
  • 2018-06-04
  • 1970-01-01
  • 2021-01-24
  • 2015-11-03
  • 2020-03-30
  • 2022-07-20
相关资源
最近更新 更多