Spring Boot Testing: Cannot Autowire springSecurityFilterChain on Test Class -
i still wresting various annotations in setting test context under spring boot.
i have been referring this article, refreshingly clear on how deal various contexts under spring boot. problem remaining cannot seem find annotation combination make springsecurityfilterchain
visible in both main application context (driven here):
@enableautoconfiguration @componentscan public class application { public static void main(string[] args) throws exception { applicationcontext ctx = springapplication.run(application.class, args); } }
and test application context begun here:
@runwith(springjunit4classrunner.class) @contextconfiguration(classes = {testpersistenceconfig.class,mvcconfig.class,securityconfig.class},loader=annotationconfigcontextloader.class) //@springapplicationconfiguration(classes = {testpersistenceconfig.class,mvcconfig.class,securityconfig.class}) @webappconfiguration public class applicationintegrationtest { mockmvc mockmvc; @autowired private webapplicationcontext wac; //@resource(name="springsecurityfilterchain") @autowired private filterchainproxy springsecurityfilterchain; @autowired private userdao userdao; @autowired private clientdao clientdao; @autowired private roledao roledao; uuid key = uuid.fromstring("f3512d26-72f6-4290-9265-63ad69eccc13"); @before public void setup() { // using web application initate mock mockmvc = mockmvcbuilders.webappcontextsetup(wac).addfilter(springsecurityfilterchain).build(); // our other choice using controller config //mockmvc = mockmvcbuilders.annotationconfigsetup(exampleapplicationcontext.class).build(); // here should build data structure using hibernate list<client> clients = new arraylist<>(); client clienten = new client(); clienten.setdeviceid("444444444"); clienten.setlanguage("en-en"); clienten.setagentid("444444444|68:5b:35:8a:7c:d0"); client clientendomain = clientdao.save(clienten); clients.add(clientendomain); list<role> roles = new arraylist<>(); role roleuser = new role(); roleuser.setrole("user"); role roleuserdomain = roledao.save(roleuser); roles.add(roleuserdomain); role roleadmin = new role(); roleadmin.setrole("admin"); role roleadmindomain = roledao.save(roleadmin); roles.add(roleadmindomain); user user = new user(); user.setlogin("user"); user.setpassword("password"); user.setclients(clients); user.setroles(roles); userdao.save(user); } @test public void thatviewbootstrapuseshttpnotfound() throws exception { // testing correct login form result in cookie being set mvcresult result = mockmvc.perform(post("/login") .param("username", "user").param("password", "password")).andreturn(); cookie c = result.getresponse().getcookie("my-cookie"); cookie[] cookies = result.getresponse().getcookies(); (int = 0; <= cookies.length; i++) { system.out.println("cookie " + + " name: " + cookies[i].getname()); system.out.println("cookie " + + " value: " + cookies[i].getvalue()); } //assertthat(c.getvalue().length(), greaterthan(10)); // no cookie; 401 unauthorized mockmvc.perform(get("/")).andexpect(status().isunauthorized()); // cookie; 200 ok mockmvc.perform(get("/").cookie(c)).andexpect(status().isok()); // logout, , ensure we're told wipe cookie result = mockmvc.perform(delete("/session")).andreturn(); c = result.getresponse().getcookie("my-cookie"); assertthat(c.getvalue().length(), is(0)); } }
by way @springapplicationconfiguration
doesn't seem work in circumstance, contrary doco. security config follows:
@configuration @enablewebmvcsecurity @componentscan({ "com.touchcorp.touchpoint.security", "com.touchcorp.touchpoint.service", "com.touchcorp.touchpoint.model.dao"}) public class securityconfig extends websecurityconfigureradapter { @autowired deviceusernamepasswordauthenticationprovider customauthenticationprovider; @override protected void configure(authenticationmanagerbuilder auth) throws exception { auth .authenticationprovider(customauthenticationprovider); } @configuration @order(1) public static class apiwebsecurityconfigurationadapter extends websecurityconfigureradapter { protected void configure(httpsecurity http) throws exception { http .antmatcher("/api/**") .authorizerequests() .anyrequest().hasrole("admin") .and() .httpbasic(); } } @order(2) @configuration public static class formloginwebsecurityconfigureradapter extends websecurityconfigureradapter { @override protected void configure(httpsecurity http) throws exception { http .csrf().disable() .authorizerequests() .anyrequest().authenticated() .and() .formlogin() .loginpage("/login") .failureurl("/login?error=1") .permitall() .and() .logout() .logouturl("/logout") .logoutsuccessurl("/"); } } public void addresourcehandlers(resourcehandlerregistry registry) { registry .addresourcehandler("/resources/**") .addresourcelocations("/resources/") .setcacheperiod(31556926); } }
can see why springsecurityfilterchain
invisible ("no beans of fileterchainproxy
type found"). thanks, i'm pulling hair out here.
i think bit unclear purpose of annotations. spring boot reference good, doesn't extend beyond established baseline. seems have combine spring security, hibernate , mvc together, starts complicated , not clear 1 do.
i worried why @springapplicationconfiguration
not working because in extensive use elsewhere (e.g. in spring boot samples) , works fine there. maybe classpath issue? how linking complete project others can try reproduce problem?
you have 2 different application contexts (one test , 1 in application
) wouldn't surprising if behaved differently. in particular application
has @enableautoconfiguration
, test (as far can see) not, there's 1 difference that's worth looking into. nothing wrong test.
here's example of test autowires security filter: https://github.com/spring-projects/spring-security-oauth/blob/master/samples/oauth2/sparklr/src/test/java/org/springframework/security/samples/config/applicationconfigurationtests.java. works. here's another: https://github.com/cloudfoundry/uaa/blob/master/uaa/src/test/java/org/cloudfoundry/identity/uaa/mock/audit/auditcheckmvcmocktests.java.
Comments
Post a Comment