最初: 私の質問はNo AuthenticationProvider found for UsernamePasswordAuthenticationTokenのようですが、そこで見つかったソリューションを実装してもそれ以上役に立ちません。
私のアプリコンテキストは次のようになります:
<?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" />
私のテストコントローラー:
@ContextConfiguration("classpath:app-context.xml")
public abstract class AbstractControllerTest extends AbstractController {
@Autowired
private AuthenticationManager am;
@Mock
private RemoteCrowdAuthenticationProvider authProvider;
@Mock
private AuthenticationProvider customAuthenticationProvider;
@After
public void clear() {
SecurityContextHolder.clearContext();
}
protected User getJohnDoe() {
// role for ordinary user (medewerker)
User user = new User("JohnDoe", "John", null, null, "Doe");
Collection<GrantedAuthority> userAuthorities = new ArrayList<GrantedAuthority>();
userAuthorities.add(new SimpleGrantedAuthority("EVERYONE_General"));
userAuthorities.add(new SimpleGrantedAuthority("EVERYONE_Profile"));
Authentication auth = new UsernamePasswordAuthenticationToken(user.getLoginID(), null, userAuthorities);
SecurityContextHolder.getContext().setAuthentication(am.authenticate(auth));
return user;
}
Eclipse内でJUNitを使用して実行する特定のテストクラス:
@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));
}
そしてモック化された UserService:
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
UserServiceMock
として使用する構成でプロジェクトを彼に言っているので、その理由がわかりませんAuthenticationProvider