package com.devcolibri.sso.dto; import java.io.Serializable; public class CASUser implements Serializable { private String username; private String password; public CASUser(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.devcolibri.sso.service; import com.devcolibri.sso.dto.CASUser; import org.jasig.cas.authentication.UsernamePasswordCredential; public interface CASUserService { CASUser getByCredential(UsernamePasswordCredential credential); }
package com.devcolibri.sso.service; import com.devcolibri.sso.dto.CASUser; import org.jasig.cas.authentication.UsernamePasswordCredential; import org.springframework.stereotype.Service; @Service public class CASUserServiceImpl implements CASUserService { @Override public CASUser getByCredential(UsernamePasswordCredential credential) { String usernameMock = "test"; String passwordMock = "test"; CASUser user = null; if(credential.getUsername().equals(usernameMock) && credential.getPassword().equals(passwordMock)) { user = new CASUser(usernameMock, passwordMock); } return user; } }
package com.devcolibri.sso.handler; import com.devcolibri.sso.dto.CASUser; import com.devcolibri.sso.service.CASUserService; import org.jasig.cas.authentication.HandlerResult; import org.jasig.cas.authentication.PreventedException; import org.jasig.cas.authentication.UsernamePasswordCredential; import org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler; import org.jasig.cas.authentication.principal.SimplePrincipal; import org.springframework.beans.factory.annotation.Autowired; import javax.security.auth.login.AccountNotFoundException; import java.security.GeneralSecurityException; public class ServerUsernamePasswordAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler { private CASUserService casUserService; @Override protected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential) throws GeneralSecurityException, PreventedException { casUserService = new CASUserServiceImpl(); CASUser user = casUserService.getByCredential(credential); if(credential.getUsername().equals(user.getUsername()) && credential.getPassword().equals(user.getPassword())) { user = new CASUser(user.getUsername(), user.getPassword()); } if (user == null) { throw new AccountNotFoundException(user.getUsername() + " not found."); } return createHandlerResult(credential, new SimplePrincipal(user.getUsername()), null); } }
<bean id="primaryAuthenticationHandler" class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler"> <property name="users"> <map> <entry key="casuser" value="Mellon"/> </map> </property> </bean>
<bean id="primaryAuthenticationHandler" class="com.devcolibri.sso.handler.ServerUsernamePasswordAuthenticationHandler" />
Source: https://habr.com/ru/post/226839/
All Articles