Spring Security, JUnit: @WithUserDetails for user created in @Before

微笑、不失礼 提交于 2019-12-04 17:28:32

问题


In JUnit tests with Spring MockMVC, there are two methods for authenticating as a Spring Security user: @WithMockUser creates a dummy user with the provided credentials, @WithUserDetails takes a user's name and resolves it to the correct custom UserDetails implementation with a custom UserDetailsService (the UserDetailsServiceImpl).

In my case, the UserDetailsService loads an user from the database. The user I want to use was inserted in the @Before method of the test suite.

However, my UserDetailsServiceImpl does not find the user.

In my @Before, I insert the user like this:

User u = new User();
u.setEMail("test@test.de");
u = userRepository.save(u);

And in the UserDetailsServiceImpl:

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = this.userRepository.findOneByEMail(username);

    if (user == null)
        throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
    return user;
}

How can I use an account created in @Before with @WithUserDetails?


回答1:


Unfortunately, you can't do easily @WithUserDetails with @Before, because Spring @WithUserDetails annotation will invoke Spring security context test listener before running setUp method with @Before.

Here is https://stackoverflow.com/a/38282258/1814524 a little trick and answer to your question.




回答2:


@Inject
private EntityManager em;

@Inject
PlatformTransactionManager txManager;

@BeforeTransaction
public void setup() {
    new TransactionTemplate(txManager).execute(status -> {

        User u = new User();
        u.setEMail("test@test.de");
        em.save(u);

        return null;
    });
}

@AfterTransaction
public void cleanup() {
    new TransactionTemplate(txManager).execute(status -> {
        // Check if the entity is managed by EntityManager.
        // If not, make it managed with merge() and remove it.
        em.remove(em.contains(u) ? user1 : em.merge(u));
        return null;
    });
}


@Test
@Transactional
@WithUserDetails(value = "test@test.de", userDetailsServiceBeanName = "loadUserByUsername")
public void test() {

}



回答3:


You can use @PostConstruct instead of @Before. That did the trick for me. Can anybody confirm that?



来源:https://stackoverflow.com/questions/42466954/spring-security-junit-withuserdetails-for-user-created-in-before

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!