Unit testing 尝试单元测试授权时with@PreAuthorize我得到;找不到UsernamePasswordAuthenticationToken“的AuthenticationProvider;
第一:我的问题似乎是这样的,但实现在那里找到的解决方案对我没有任何帮助 我的应用程序上下文如下所示:Unit testing 尝试单元测试授权时with@PreAuthorize我得到;找不到UsernamePasswordAuthenticationToken“的AuthenticationProvider;,unit-testing,junit,spring-security,authorization,mockito,Unit Testing,Junit,Spring Security,Authorization,Mockito,第一:我的问题似乎是这样的,但实现在那里找到的解决方案对我没有任何帮助 我的应用程序上下文如下所示: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="userService" />
</security:authentication-manager>
<bean id="userService" class="<util-classpath>.UserServiceMock" >
<property name="userDetailsService" ref="crowdUserDetailsService" />
<property name="requestHelper" ref="mockRequestHelper" />
</bean>
<bean id="mockRequestHelper" class="<util-classpath>.RequestHelperMock" />
<bean id="crowdUserDetailsService" class="<util-classpath>.UserDetailsServiceMock" />
<bean id="RemoteCrowdAuthenticationProvider" class="<util-classpath>.RemoteCrowdAuthenticationProviderMock">
<constructor-arg ref="crowdAuthenticationManager" />
<constructor-arg ref="httpAuthenticator" />
<constructor-arg ref="crowdUserDetailsService" />
</bean>
<bean id="crowdAuthenticationManager" class="<util-classpath>.CrowdAuthenticationManagerMock" >
<constructor-arg ref="securityServerClient" />
</bean>
<bean id="securityServerClient" class="<util-classpath>.SecurityServerClientMock" >
<constructor-arg ref="clientProperties" />
</bean>
<bean id="clientProperties" class="<util-classpath>.ClientPropertiesMock" >
<constructor-arg ref="properties" />
</bean>
<bean id="properties" class="<util-classpath>.PropertiesMock" />
<bean id="httpAuthenticator" class="<util-classpath>.HttpAuthenticatorMock" >
<constructor-arg ref="clientProperties" />
</bean>
<security:global-method-security pre-post-annotations="enabled" />
我不明白为什么我告诉配置中的项目使用
UserServiceMock
作为AuthenticationProvider
实际上我所要做的就是在UserServiceMock中实现以下方法:
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
return authentication;
}
而不是返回null 为什么需要在
getJohnDoe()
方法中设置SecurityContextHolder
?这就是我在这里发现的:[,我需要使用spring@PreAuthorize注释测试具有不同授权级别的不同用户。您试图做的是集成和验证测试之间的事情。绝对不是单元测试。
@RunWith(SpringJUnit4ClassRunner.class)
public class AdministrationControllerTest extends AbstractControllerTest {
@Mock
private UserService userService;
@Mock
private RequestHelper requestHelper;
@InjectMocks
private AdministrationController controller = new AdministrationController();
private List<User> users;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
users = new ArrayList<User>();
User employee = new User("smithb", "Bob", "", "", "Smith", User.SeniorityLevel.MEDIOR, "Medior developer", initProfile());
employee.setId(new Long(123));
users.add(employee);
when(userService.findAllUsersIncludingInactive(new Sort(new Sort.Order("firstName")))).thenReturn(users);
}
@Test
public void testShowCompleteEmployeeOverviewNoAccess() {
User johnDoe = getJohnDoe();
ModelAndView modelAndView = controller.showCompleteEmployeeOverview(johnDoe, new ModelAndView());
assertEquals(Constants.LOGOUT_URL, modelAndView.getViewName());
verify(userService, never()).findAllUsersIncludingInactive(any(Sort.class));
}
public final class UserServiceMock implements UserService, AuthenticationProvider {
private UserDetailsServiceMock userDetailsService;
private RequestHelperMock requestHelper;
@Override
public List<User> findAllUsers() {
// TODO Auto-generated method stub
return null;
}
@Override
public List<User> findAllUsers(Sort sort) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<User> findAllUsersIncludingInactive(Sort sort) {
// TODO Auto-generated method stub
return null;
}
@Override
public User findUserById(Long id) {
// TODO Auto-generated method stub
return null;
}
@Override
public User findUserByLoginID(String loginId) {
// TODO Auto-generated method stub
return null;
}
@Override
public User createUser() {
// TODO Auto-generated method stub
return null;
}
@Override
public User saveUser(User user) {
// TODO Auto-generated method stub
return null;
}
@Override
public void removeUser(User user) {
// TODO Auto-generated method stub
}
@Override
public Team findTeamById(Long id) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Team> findAllTeams() {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Team> findAllTeams(Sort sort) {
// TODO Auto-generated method stub
return null;
}
@Override
public Team saveTeam(Team team) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<User> searchUsers(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean supports(Class<?> authentication) {
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
public UserDetailsServiceMock getUserDetailsService() {
return userDetailsService;
}
public void setUserDetailsService(UserDetailsServiceMock userDetailsService) {
this.userDetailsService = userDetailsService;
}
public RequestHelperMock getRequestHelper() {
return requestHelper;
}
public void setRequestHelper(RequestHelperMock requestHelper) {
this.requestHelper = requestHelper;
}
}
org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
return authentication;
}