You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.6 KiB
81 lines
2.6 KiB
4 years ago
|
package com.fr.plugin.sunac.sso;
|
||
|
|
||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
|
||
|
import com.fr.general.PropertiesUtils;
|
||
|
import com.fr.plugin.transform.FunctionRecorder;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
|
||
|
import javax.servlet.FilterChain;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
import java.util.Properties;
|
||
|
import java.util.UUID;
|
||
|
|
||
|
import static com.fr.plugin.sunac.sso.CommonUtils.*;
|
||
|
|
||
|
/**
|
||
|
* @author fr.open
|
||
|
* @since 2021/8/26
|
||
|
*/
|
||
|
@FunctionRecorder
|
||
|
public class FormLoginFilter extends AbstractGlobalRequestFilterProvider {
|
||
|
|
||
|
@Override
|
||
|
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
|
||
|
// 已登录
|
||
|
if (isLogin(request)) {
|
||
|
next(request, response, chain);
|
||
|
return;
|
||
|
}
|
||
|
String username = request.getParameter("fs-login-username");
|
||
|
// 没有找到表单用户名
|
||
|
if (StringUtils.isBlank(username)) {
|
||
|
next(request, response, chain);
|
||
|
return;
|
||
|
}
|
||
|
try {
|
||
|
getUser(username);
|
||
|
login(username, request, response);
|
||
|
next(request, response, chain);
|
||
|
} catch (Exception e) {
|
||
|
try {
|
||
|
jumpAuthorize(request, response);
|
||
|
} catch (IOException ex) {
|
||
|
throw new RuntimeException(ex);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void jumpAuthorize(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||
|
Properties props = PropertiesUtils.getProperties("sunac");
|
||
|
String apiAuthorize = getProperty(props, "api.authorize", false);
|
||
|
String apiClientId = getProperty(props, "api.client_id", false);
|
||
|
String apiRedirectURI = getProperty(props, "api.redirect_uri", false);
|
||
|
String state = UUID.randomUUID().toString();
|
||
|
String accessURL = request.getRequestURI();
|
||
|
if (StringUtils.isNotBlank(request.getQueryString())) {
|
||
|
accessURL += "?" + request.getQueryString();
|
||
|
}
|
||
|
Map<String, String> params = new HashMap<>();
|
||
|
params.put("accessURL", accessURL);
|
||
|
cacheParams(state, params);
|
||
|
String pattern = "%s?client_id=%s&redirect_uri=%s&response_type=code&scope=UserProfile.me&state=%s";
|
||
|
pattern = String.format(pattern, apiAuthorize, apiClientId, apiRedirectURI, state);
|
||
|
response.sendRedirect(pattern);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String filterName() {
|
||
|
return "formLogin";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String[] urlPatterns() {
|
||
|
return new String[]{"/*"};
|
||
|
}
|
||
|
}
|