Struts開発でOpenID Connectのユーザログイン情報取得の実現方法

Javaコード下記
public class OpenIdServiceAction extends BaseAction {
private ParameterList requestParameters;
private String returntoUrl;

private static final long serialVersionUID = xxx;

public String execute() throws Exception {
Map parameterMap = request.getParameterMap();
if (parameterMap.isEmpty()) {
// Processing empty request. Assuming discovery request…
OpenIdProviderService.sendDiscoveryResponse(response);
} else {
requestParameters = new ParameterList(request.getParameterMap());
OpenIdProviderService.logRequestParameters(requestParameters);
String mode = requestParameters.hasParameter(“openid.mode”) ? requestParameters
.getParameterValue(“openid.mode”) : null;
System.out.println(“Processing OpenID request ‘” + mode + “‘…”);
// Save off the return_to value so when the user logs in
// successfully,
// we can redirect the browser there…
// Crack the Request mode and process it accordingly…
if (“associate”.equals(mode)) {
OpenIdProviderService.processAssociationRequest(response,
requestParameters);
} else if (“checkid_immediate”.equals(mode)
|| “checkid_setup”.equals(mode)
|| “check_authentication”.equals(mode)) {
// Check Session. If information is there, we’re done. No need
// to login again.
// Create AuthResponse from session variables…

// セッションログインの判断
MakotoOpenIdAwareSession mysession = MakotoOpenIdAwareSession.getInstance(session);

// Check Session. If information is there, we’re done.
// No need to login again.
if (mysession.isLoggedIn() ) {
// Create AuthResponse from session variables…
System.out.println(“********************************”);
System.out.println(“* User is already logged in…..*”);
System.out.println(“********************************”);
returntoUrl = sendSuccessfulResponse(session);
System.out.println(“returntoUrl=====” + returntoUrl);
return “returnto”;
} else {
// login
mysession.setRequestParameters(requestParameters);
return “login”;
}
} else {
System.out.println(“Unknown request mode ‘” + mode
+ “‘… Forcing login…”);
}
}
return null;
}
public String openid() throws Exception {
Map parameterMap = request.getParameterMap();
// if (parameterMap.isEmpty()) {
System.out.println(“Processing empty request. Assuming discovery request…”);
OpenIdProviderService.sendDiscoveryResponse(response);
// }
return null;
}

/**
* Build an AuthResponse to send back to the requester. Use the
* OpenIdProviderService to encapsulate this logic.
*/
private String sendSuccessfulResponse(HttpSession session) {
//System.out.println(“sendSuccessfulResponse() BEGIN…”);
MakotoOpenIdAwareSession mysession = MakotoOpenIdAwareSession
.getInstance(session);
Message authResponse = OpenIdProviderService.buildAuthResponse(
requestParameters, mysession.getUserSelectedId(),
mysession.getUserSelectedClaimedId(),
mysession.getRegistrationModel());

//System.out.println(“sendSuccessfulResponse() END…”);
return authResponse.getDestinationUrl(true);
}
// ログイン情報の取得
public String login() throws Exception {
String userId = request.getParameter(“userId”);
String password = request.getParameter(“password”);
// Validate login.
boolean authenticatedAndApproved = validateLogin(userId, password);
// If invalid, display a message
if (!authenticatedAndApproved) {
System.out.println(“ログイン失敗”);
return ERROR;

} else {

MakotoOpenIdAwareSession mysession = MakotoOpenIdAwareSession
.getInstance(session);

if (mysession.getRequestParameters().hasParameter(“openid.realm”)) {
String realm = mysession.getRequestParameters()
.getParameterValue(“openid.realm”);
String domain = getDomainName(realm);
request.setAttribute(“domain”, domain);
}
return SUCCESS;
}
}

// ログイン許可
public String returnTo() throws Exception {
String userId = “changfatest”;
// 标识码
String userSelectedId = mapUserIdToUserSelectedId(userId);
//
String userSelectedClaimedId = mapSelectedIdToClaimedId(userId);
RegistrationModel registrationModel = getRegistrationModel(userSelectedId);
// create auth response and redirect
MakotoOpenIdAwareSession mysession = MakotoOpenIdAwareSession
.getInstance(session);

Message authResponse = OpenIdProviderService.buildAuthResponse(
mysession.getRequestParameters(), userSelectedId,
userSelectedClaimedId, registrationModel);

mysession.setLoggedIn(true);
mysession.setUserSelectedId(userSelectedId);
mysession.setUserSelectedClaimedId(userSelectedClaimedId);
mysession.setRegistrationModel(registrationModel);

returntoUrl = authResponse.getDestinationUrl(true);

System.out.println(“authResponse.getDestinationUrl(true)==== ”
+ returntoUrl);

return “returnto”;

}

protected RegistrationModel getRegistrationModel(String userSelectedId) {
//System.out.println(“getRegistrationModel() BEGIN…”);
// Ideally, you would read this from a DB. Hardcode for the sample
// app…
RegistrationModel ret = new RegistrationModel();
ret.setDateOfBirth(new YearMonthDay(1982, 9, 10).toDateMidnight()
.toDate());
ret.setEmailAddress(“sample@arkgame.com”);
ret.setFavoriteColor(“blue”);
ret.setFullName(“startnews24 study it skill”);
ret.setOpenId(OpenIdProviderService.getOpEndpointUrl());
ret.setZipCode(“xxx”);
return ret;
}
protected String mapUserIdToUserSelectedId(String userId) {
//System.out.println(“mapUserIdToUserSelectedId() BEGIN…”);
//System.out.println(“mapUserIdToUserSelectedId() END…”);
return getServerUrl() + “/openid/openid?id=xxx”;
}

protected String mapSelectedIdToClaimedId(String userId) {
//System.out.println(“mapSelectedIdToClaimedId() BEGIN…”);
// Ideally, you would read this from a DB, or LDAP, or somewhere,
// / but this is sample code… just return the userid passed in…
//System.out.println(“mapSelectedIdToClaimedId() END…”);
return getServerUrl() + “/openid/openid?id=xx”;
}

/**
* ユーザーの認証
*
* @param user
* @param password
* @return
*/
private boolean validateLogin(String user, String password) {
// TODO
return true;
}

public ParameterList getRequestParameters() {
return requestParameters;
}

public void setRequestParameters(ParameterList requestParameters) {
this.requestParameters = requestParameters;
}

public String getReturntoUrl() {
return returntoUrl;
}

public void setReturntoUrl(String returntoUrl) {
this.returntoUrl = returntoUrl;
}

}

Comments are closed.