java - Spring Boot Rest 响应返回 login.jsp

标签 java spring spring-boot spring-mvc spring-security

enter image description here enter image description here

我正在学习 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/

相关文章:

java - 在 RecyclerView 中显示项目

java - 添加jlabel组件后的Jpanel大小

java - Jaxb 解码返回空值

spring - 如何将 Kerberos 票证传递给 Spring Yarn 应用程序

java - 使用 Oath 对 Google 网站中的 contentfeed 执行查询时 token 无效

java - 我应该如何存储用户凭据,以便用户不必再次登录?

java - Spring Mongo Query,删除集合匹配列表中的对象

java - Spring JPA 与 ApplicationContext.xml、DAO 和 Service 为 NULL

java - 如何将 Spring Boot 与 MySQL 数据库和 JPA 一起使用?

java - Spring 响应中 JSON 中位置 0 处出现意外标记 S