我對 Spring 測驗完全陌生,我不知道我在這里缺少什么。我有以下要測驗的控制器類。我也有 Spring Security,并且我已經從 antMatchers 中排除了該控制器,以便重定向到登錄頁面。我在下面寫了測驗,但我收到了這個問題標題的訊息。我應該怎么辦?
控制器測驗
@Autowired
private MockMvc mockMvc;
@Test
@DisplayName("Testing the Verification Complete view of the application")
public void testVerificationCompletePage() throws Exception{
mockMvc.perform(get("/verification-complete"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("login"));
}
控制器類
@GetMapping("/verification-complete")
public String showVerificationCompleteForm(){
return "verification-complete";
}
春季安全配置
package com.andrekreou.iot.authentication.security;
import com.andrekreou.iot.authentication.user.ApplicationUserService;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@AllArgsConstructor
@EnableWebSecurity
public class ApplicationSecurityConfig {
private final ApplicationUserService applicationUserService;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.requiresChannel()
.antMatchers("/actuator/prometheus")
.requiresInsecure()
.and()
.authorizeRequests()
.antMatchers(
"/api/v*/registration/**",
"/register*",
"/login",
"/registration",
"/registration-complete",
"/actuator/prometheus").permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("email")
.permitAll()
.defaultSuccessUrl("/",true)
.failureUrl("/login-error")
.and()
.logout()
.logoutUrl("/logout")
.clearAuthentication(true)
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID","Idea-2e8e7cee")
.logoutSuccessUrl("/login");
return http.build();
}
uj5u.com熱心網友回復:
您應該使用我相信的內置方法進行檢查:
mockMvc.perform(get("/verification-complete"))
.andExpect(redirectedUrl("/login"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/512465.html
