我在我的應用程式中使用 Spring 安全性來加密資料庫中的用戶密碼。由于這個原因,我在啟動應用程式時在我的 Spring 引導日志中使用生成的安全密碼: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);
}
}
因此,在啟動 Spring Boot 應用程式時不會生成密碼,但是當我從郵遞員那里點擊 api 時,它給了我 404 Not found 其他所有 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 并且不會在我的郵遞員對任何 api 的回應中出現 404 not found 錯誤。提前致謝!
uj5u.com熱心網友回復:
從 @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();
uj5u.com熱心網友回復:
您的設定實際上需要身份驗證 -.anyRequest().authenticated();您可以對任何請求都允許,應該這樣做。
http.authorizeRequests().anyRequest().permitAll();
uj5u.com熱心網友回復:
我找到了一個解決方案, 404 not found 錯誤是因為我沒有在我的一個模型類中使用 @Id 注釋來注釋欄位。現在已經解決了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511707.html
