简体   繁体   中英

Spring Boot Rest response returning login.jsp

在此处输入图像描述 在此处输入图像描述

I am learning Spring Boot. I created a sample Spring Boot Web Application with Spring Boot security and it is working perfectly.
Now I want to add Rest Webservices functionality in same web application.

When i am trying to access any API, in response I am getting login.jsp.当我尝试访问任何 API 时,我得到 login.jsp 作为响应。
I want JSON respnse authentication and authorization not required.我想要 JSON 响应身份验证和授权不需要。
What changes I need to do to achieve this.



@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());
    }
}

Add that path to antMatchers to permitAll,

antMatchers("/resources/**", "/registration", "/api/user/**").permitAll()

this will disable security for that URL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM