如何解决运行测试
我正在使用Spring Security创建一个Spring Boot应用程序。我创建了一个自定义登录页面,该页面使用AuthController
中的auth端点。我的SecurityConfiguration
类扩展了WebSecurityConfigurerAdapter
。
目前,我正在尝试为此创建测试,但没有任何结果。其中大多数对我来说都是新手,我仍在学习,特别是测试。我什至不确定我是否正确地做到了。
感谢您的帮助。
安全配置
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private final PasswordEncoder passwordEncoder;
private final ApplicationUserService applicationUserService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers( "/register","/index*","/static/**","/*.js","/*.json","/*.ico").permitAll()
.antMatchers(HttpMethod.POST,"/api/auth/**").permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login").permitAll()
//.loginProcessingUrl("/api/auth/authenticate")
.defaultSuccessUrl("/home",true);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(daoAuthenticationProvider());
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(passwordEncoder);
provider.setUserDetailsService(applicationUserService);
return provider;
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
授权控制器
@RequiredArgsConstructor
@RestController
@RequestMapping("api/auth/")
public class AuthController {
private final UserService userService;
@PostMapping("authenticate")
public ResponseEntity<?> verifyUser(@RequestBody User user) throws Exception {
return userService.authenticateUser(user);
}
}
USERSERVICE
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
private final AuthenticationManager authenticationManager;
public ResponseEntity<?> authenticateUser(User user) throws Exception {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(user.getEmail(),user.getPassword()));
if (!authentication.isAuthenticated())
return new ResponseEntity<>(new ResponseObject("Error"),HttpStatus.UNAUTHORIZED);
else {
SecurityContextHolder.getContext().setAuthentication(authentication);
return new ResponseEntity<>(user,HttpStatus.OK);
}
}
}
TESTCLASS
@WebMvcTest(AuthController.class)
class AuthControllerTest {
User user;
@MockBean
UserService userService;
@Autowired
MockMvc mockMvc;
@BeforeEach
void setUp() {
user = User.builder()
.email("test@hotmail.com")
.password("password")
.id(1L).build();
}
@Test
void verifyUser() throws Exception {
given(userService.authenticateUser(any(User.class))).willReturn(new ResponseEntity<>(HttpStatus.OK));
mockMvc.perform(post("/api/auth/authenticate")
.contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user)))
.andExpect(status().isOk());
}
}
错误
无法加载ApplicationContext java.lang.IllegalStateException:无法加载ApplicationContext原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建文件[\ SecurityConfiguration.class]中定义的名称为'securityConfiguration'的bean时出错:依赖关系未满足通过构造函数参数0表示;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'org.springframework.security.crypto.password.PasswordEncoder'的合格bean:期望至少有1个有资格作为自动装配候选的bean。依赖项注释:{}由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为“ org.springframework.security.crypto.password.PasswordEncoder”的合格Bean:期望至少有1个有资格作为自动装配候选的Bean。依赖项注释:{}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。