简体   繁体   中英

Testing spring-boot security, Thymeleaf error

I'm using Thymeleaf with Spring-boot and I get a problem testing the security @withmockuser.

This is the code for testing the controller

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SuppressWarnings("SpringJavaAutowiringInspection")
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = SecurityConfiguration.class)
@WebMvcTest(IndexController.class)
public class IndexControllerTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private IndexController IndexController;

    @Test
    @WithMockUser
    public void testAuthenticated() throws Exception {
        this.mvc.perform(get("/"))
                .andExpect(status().is(200));

    }

}

And this is the controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class IndexController {
    private DemoshopService demoshopService;

    @Autowired
    public void setDemoshopService(DemoshopService demoshopService) {
        this.demoshopService = demoshopService;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("demoshops", demoshopService.listAllDemoshops());
        return "index";
    }

}

It will give me the following error

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception processing template ()

Does this mean Thymeleaf reads "/" as an template? Without @withMockUser it does what I'm expecting.

Thank you for the help.

EDIT: as requested the security configuration:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/images/**").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()                                    
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("test1").password("password").roles("USER")
                .and()
                .withUser("test2").password("password").roles("USER")
                .and()
                .withUser("test3").password("password").roles("USER")
                .and()
                .withUser("test4").password("password").roles("USER")
                .and()
                .withUser("test5").password("password").roles("USER")
                .and()
                .withUser("test6").password("password").roles("USER");
    }
}

The usernames are normally e-mail address but I changed them for now.

Without parameters @WithMockUser will run test with the username "user", the password "password", and the roles "ROLE_USER".

Use @WithMockUser(username="test1",roles={"USER"}) to run test for test1 user for example.

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