我在我的应用程序中使用Spring安全来加密数据库中的用户密码。因此,在启动应用程序时,我将使用生成的安全密码:XXXXXX在我的Spring日志中获得。我不希望生成这个密码,所以我在我的主类中使用@SpringBootApplication ( main = {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);
        }
}因此,在启动spring引导应用程序时不会生成密码,但是当我从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,而不是得到404号没有发现错误在我的邮递员在任何api.Thanks的反应提前!!
发布于 2022-10-09 03:44:50
我找到了一个解决方案,404没有找到错误是因为我没有在我的模型类中用@Id注释注释字段。现在已经解决了。
发布于 2022-10-08 07:02:15
从@SpringBootApplication和。在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();发布于 2022-10-08 07:34:53
您的设置实际上需要身份验证--您可以为任何请求做所有的.anyRequest().authenticated();,这应该是允许的。
http.authorizeRequests().anyRequest().permitAll();https://stackoverflow.com/questions/73994990
复制相似问题