postman 排除自动配置类会出现404未找到错误

ddrv8njm  于 2022-11-07  发布在  Postman
关注(0)|答案(3)|浏览(167)

我在我的应用程序中使用Spring安全性来加密数据库中的用户密码。因此,启动应用程序时,我在Sping Boot 日志中收到***使用生成的安全密码:XXXXXX***。我不希望生成此密码,因此我在主类中使用@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })。以下是我的主类

package com.example.policymanagementsystem;
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication (exclude = {SecurityAutoConfiguration.class })
@ComponentScan( basePackages = {"com.example.policymanagementsystem.bean"})
public class PolicymanagementsystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(PolicymanagementsystemApplication.class, args);
        }
}

因此,在启动Sping Boot 应用程序时不会生成密码,但当我从postman中点击api时,它会给我404未找到所有其他api的错误。我不希望对我的任何api进行身份验证,因此我在SecurityConfiguration类中给出了以下配置。

@Override
    protected void configure(HttpSecurity http) throws Exception {

        http.cors().and().csrf().disable();
        http.authorizeRequests().antMatchers("/admin/**").permitAll().antMatchers("/user/**").permitAll().anyRequest().authenticated();

http.exceptionHandling()
        .authenticationEntryPoint(
            (request, response, ex) -> {
                response.sendError(
                    HttpServletResponse.SC_UNAUTHORIZED,
                    ex.getMessage()
                );
            }
        );
 http.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class);
}

请建议我一些方法,这样我就可以排除SecurityAutoConfiguration.class,而不会在我的 Postman 响应中出现404未找到错误。提前感谢!!

fslejnso

fslejnso1#

从@SpringBootApplication中移除exclude部分,并在www.example.com档案中加入这两行application.properties。

spring.security.user.name=abc
spring.security.user.password=xxx

请尝试将此代码用于SecurityConfiguration。并删除。anyRequest()。authenticated();因为你不需要。

http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/admin").hasAuthority("ADMIN")
//                .antMatchers("/post").authenticated()
                .antMatchers("/post","/deleteComment/**", "/deletePost/**", "/updateComment/**",
                        "/updateCommentPage/**", "/updatePostPage/**","/api/draft/**","/api/post/**").hasAnyAuthority("USER","ADMIN")
                .antMatchers("/","/api","/api/addComment/**","/api/viewPost/**").permitAll()
                .and().formLogin()
                .loginPage("/login").permitAll()
                .and().httpBasic().and()
                .logout().invalidateHttpSession(true)
                .clearAuthentication(true)
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/logout-success").permitAll();
zsohkypk

zsohkypk2#

您的设置实际上需要身份验证-.anyRequest().authenticated();您可以对任何请求都允许,这样就可以了。

http.authorizeRequests().anyRequest().permitAll();
zbq4xfa0

zbq4xfa03#

我找到了一个解决方案,404找不到错误是因为我没有用@Id注解我的一个模型类中的字段。现在已经解决了。

相关问题