Categories
Java SpringBoot

SpringBoot DisableSecurityConfiguration

Lets say you have a SpringBoot application and you want to test some functionality but this application is a consumer and you are dependent on Kafka queue or SQS Queue .
In this case you can create a controller and disable the security and call the function in your controller and call this controller using postman .

Code to disable security

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@Order(1)
public class DisableSecurityConfigurationAdapater extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().antMatcher("/**").authorizeRequests().anyRequest().permitAll();
    }
}

Code for controller (Something like this)

import com.dharmeshpatel.integration.Scheduled.Scheduler;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/export/")
public class ABCController {

    @Resource
    Scheduler scheduler1;

    @RequestMapping(value = "/startReport", method = RequestMethod.POST)
    public ResponseEntity<String> startExport(@RequestParam(value = "startDate") String startDate,
                                              @RequestParam(value = "endDate") String endDate) {
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            scheduler1.startExport(startDate, endDate);
            return new ResponseEntity<>("The export WritesReport has started", HttpStatus.OK);
        } else {
            return new ResponseEntity<>(
                "The export WritesReport has not started, startDate expected in format dd-MM-yy",
                HttpStatus.BAD_REQUEST);
        }
    }
}



Leave a Reply