我正在学习 Spring Boot。我创建了一个具有 Spring Boot 安全性的示例 Spring Boot Web 应用程序,并且它运行良好。
现在我想在同一个 Web 应用程序中添加 Rest Webservices 功能。< br/>
问题:
当我尝试访问任何 API 时,我得到的响应是 login.jsp 。
要求:我想要 JSON 响应身份验证和授权不需要。
为了实现这一目标,我需要做哪些改变。
WebSecurityConfig:
@Configuration
@EnableWebSecurity <br>
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Qualifier("userDetailsServiceImpl") <br>
@Autowired <br>
private UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**", "/registration").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Bean
public AuthenticationManager customAuthenticationManager() throws Exception {
return authenticationManager();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
}
最佳答案
将该路径添加到antMatchers
以permitAll,
antMatchers("/resources/**", "/registration", "/api/user/**").permitAll()
这将禁用该 URL 的安全性
关于java - Spring Boot Rest 响应返回 login.jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61044553/