首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >排除自动配置类会导致404未找到错误。

排除自动配置类会导致404未找到错误。
EN

Stack Overflow用户
提问于 2022-10-08 06:50:42
回答 3查看 80关注 0票数 1

我在我的应用程序中使用Spring安全来加密数据库中的用户密码。因此,在启动应用程序时,我将使用生成的安全密码:XXXXXX在我的Spring日志中获得。我不希望生成这个密码,所以我在我的主类中使用@SpringBootApplication ( main = {SecurityAutoConfiguration.class })。下面的是我的主要类

代码语言:javascript
运行
复制
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);
        }
}

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

代码语言:javascript
运行
复制
@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,而不是得到404号没有发现错误在我的邮递员在任何api.Thanks的反应提前!!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-10-09 03:44:50

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

票数 0
EN

Stack Overflow用户

发布于 2022-10-08 07:02:15

从@SpringBootApplication和。在application.properties文件中添加这两行。

代码语言:javascript
运行
复制
spring.security.user.name=abc
spring.security.user.password=xxx

试试SecurityConfiguration的这段代码。并删除.anyRequest().authenticated();因为您不需要这样做。

代码语言:javascript
运行
复制
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();
票数 1
EN

Stack Overflow用户

发布于 2022-10-08 07:34:53

您的设置实际上需要身份验证--您可以为任何请求做所有的.anyRequest().authenticated();,这应该是允许的。

代码语言:javascript
运行
复制
http.authorizeRequests().anyRequest().permitAll();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73994990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档