2012-08-09 151 views
0

有沒有一種方法可以避免使用acegi安全模塊的servlet應用程序中基於表單的登錄?我想通過某種方式將登錄名和密碼傳遞給URL。我試着打電話給這樣的事情:自動登錄acegi安全

GrantedAuthority[] grantedAuthorities = new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ADMIN")}; 
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("admin", "passwordofadmin", grantedAuthorities); 
WebAuthenticationDetails authdetails = (WebAuthenticationDetails) authentication.getDetails(); 
HttpSession session = event.getSession(); 
ProviderManager pm = (ProviderManager)WebApplicationContextUtils.getWebApplicationContext(session.getServletContext()).getBean("authenticationManager"); 
pm.doAuthentication(authentication); 
SecurityContextHolder.getContext().setAuthentication(authentication); 
HttpSessionCreatedEvent事件的處理程序

,我收到AuthenticationSuccessEvent事件,但隨後Failure事件還接收並顯示認證形式。

回答

0

首先,您必須重寫xml中的UserDetailsS​​ervice。事情是這樣的:

<bean id="userService" class="com.security.UserSecurityService" > 
    ... 
</bean> 

然後寫自己的service.like:

public class UserSecurityService implements UserDetailsService { 
public UserDetails loadUserByUsername(String username) 
     throws UsernameNotFoundException, DataAccessException{ 

User user =userDAO.findByUsername(username); 
GrantedAuthority[] grantedRoles = {new GrantedAuthorityImpl("ROLE_USER")}; 

     secUsr = new User(user.getUsername(), user.getPassword(), true, grantedRoles); 

我希望這將有助於你