com.aluka.nirvana.framework.security.handler.CustomAuthenticationSuccessHandler Maven / Gradle / Ivy
package com.aluka.nirvana.framework.security.handler;
import cn.hutool.json.JSONObject;
import com.aluka.nirvana.framework.security.configuration.SecurityCustomsConfiguration;
import com.aluka.nirvana.framework.security.constant.Constants;
import com.aluka.nirvana.framework.security.constant.ResultJsonKey;
import com.aluka.nirvana.framework.security.principal.AuthenticationSuccessResponse;
import com.aluka.nirvana.framework.security.principal.BaseUserDetails;
import com.aluka.nirvana.framework.security.utils.JwtUtils;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;
/**
* 自定义 Security 登陆成功处理类
* @author gongli
* @since 2019/4/10 13:40
*/
@Component
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Autowired
private SecurityCustomsConfiguration customsConfiguration;
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Authentication authentication) throws IOException, ServletException {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR, customsConfiguration.getTokenExpire());
// 创建 Json Web Token
BaseUserDetails userDetails = (BaseUserDetails) authentication.getPrincipal();
String loginSuccessCustomFields = customsConfiguration.getLoginSuccessCustomFields();
String token = JwtUtils.create(userDetails, calendar.getTime(), loginSuccessCustomFields);
// 封装返回数据
AuthenticationSuccessResponse response = new AuthenticationSuccessResponse();
response.setStatus(HttpStatus.OK.value());
response.setMessage("登录成功!");
response.setToken(Constants.TOKEN_PREFIX + token);
response.setScope(userDetails.getAuthorities());
response.setExpire(calendar.getTime().getTime());
response.setCustomField(bindCustomField(userDetails));
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.getWriter().write(response.toString());
}
private Map bindCustomField(BaseUserDetails userDetails) {
Map customFields = Maps.newHashMap();
String loginSuccessCustomFields = customsConfiguration.getLoginSuccessCustomFields();
for (String customField : StringUtils.split(loginSuccessCustomFields, ",")) {
Object customFieldValue = userDetails.getCustomField(customField);
if(customFieldValue != null){
customFields.put(customField, customFieldValue);
}
}
return customFields;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy