Skip to content

Commit

Permalink
sessionUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Brant Hwang committed Jul 26, 2017
1 parent a700f97 commit 7ffb36b
Show file tree
Hide file tree
Showing 7 changed files with 519 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ${basePackage}.domain.user;

import eu.bitwalker.useragentutils.*;
import lombok.Data;

@Data
public class MDCLoginUser {
private SessionUser sessionUser;

private UserAgent userAgent;

private BrowserType browserType;

private RenderingEngine renderingEngine;

private DeviceType deviceType;

private Manufacturer manufacturer;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package ${basePackage}.domain.user;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

@Data
public class SessionUser implements UserDetails {

private String userCd;

private String userPs;

private String email;

private String userNm;

private Locale locale;

private String timeZone;

private String menuGrpCd;

private String dateFormat;

private String dateTimeFormat;

private String timeFormat;

private String menuHash;

private long expires;

private Map<String, Object> details = new HashMap<>();

public String getDetailByString(String key) {
return getDetail(key) == null ? "" : (String) getDetail(key);
}

public Object getDetail(String key) {
if (details.containsKey(key)) {
return details.get(key);
}
return null;
}

public void addDetails(String key, String value) {
details.put(key, value);
}

private List<String> authorityList = new ArrayList<>();

private List<String> authGroupList = new ArrayList<>();

@Override
@JsonIgnore
public Collection<? extends GrantedAuthority> getAuthorities() {
List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
authorityList.forEach(role -> simpleGrantedAuthorities.add(new SimpleGrantedAuthority(role)));
return simpleGrantedAuthorities;
}

public void addAuthority(String role) {
authorityList.add("ROLE_" + role);
}

public boolean hasRole(String role) {
return authorityList.stream().filter(a -> a.equals("ROLE_" + role)).findAny().isPresent();
}

@Override
@JsonIgnore
public String getPassword() {
return userPs;
}

@Override
@JsonIgnore
public String getUsername() {
return userCd;
}

@Override
@JsonIgnore
public boolean isAccountNonExpired() {
return true;
}

@Override
@JsonIgnore
public boolean isAccountNonLocked() {
return true;
}

@Override
@JsonIgnore
public boolean isCredentialsNonExpired() {
return true;
}

@Override
@JsonIgnore
public boolean isEnabled() {
return true;
}

public void setExpires(long expires) {
this.expires = expires;
}

public void addAuthGroup(String grpAuthCd) {
authGroupList.add(grpAuthCd);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package ${basePackage}.logging;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import com.chequer.axboot.core.config.AXBootContextConfig;
import com.chequer.axboot.core.domain.log.AXBootErrorLog;
import com.chequer.axboot.core.domain.log.AXBootErrorLogService;
import com.chequer.axboot.core.utils.JsonUtils;
import lombok.Getter;
import lombok.Setter;
import net.gpedro.integrations.slack.SlackApi;
import net.gpedro.integrations.slack.SlackAttachment;
import net.gpedro.integrations.slack.SlackField;
import net.gpedro.integrations.slack.SlackMessage;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Setter
@Getter
public class AXBootLogbackAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {

private AXBootErrorLogService errorLogService;

private AXBootContextConfig axBootContextConfig;

private AXBootContextConfig.Logging axBootLoggingConfig;

public AXBootLogbackAppender(AXBootErrorLogService axBootErrorLogService, AXBootContextConfig axBootContextConfig) {
this.errorLogService = axBootErrorLogService;
this.axBootContextConfig = axBootContextConfig;
this.axBootLoggingConfig = axBootContextConfig.getLoggingConfig();
}

@PostConstruct
public void created() {
this.axBootLoggingConfig = axBootContextConfig.getLoggingConfig();
}

@Override
public void doAppend(ILoggingEvent eventObject) {
super.doAppend(eventObject);
}

@Override
protected void append(ILoggingEvent loggingEvent) {
if (loggingEvent.getLevel().isGreaterOrEqual(axBootLoggingConfig.getLevel())) {
AXBootErrorLog errorLog = errorLogService.build(loggingEvent);

if (axBootLoggingConfig.getDatabase().isEnabled()) {
if (axBootLoggingConfig.getSlack().isEnabled()) {
errorLog.setAlertYn("Y");
}
toDatabase(errorLog);
}

if (axBootLoggingConfig.getSlack().isEnabled()) {
toSlack(errorLog);
}

if (axBootLoggingConfig.getJandi().isEnable()) {
toJandi(errorLog);
}
}
}

private void toJandi(AXBootErrorLog errorLog) {

}

private void toSlack(AXBootErrorLog errorLog) {
SlackApi slackApi = new SlackApi(axBootLoggingConfig.getSlack().getWebHookUrl());

List<SlackField> fields = new ArrayList<>();

SlackField message = new SlackField();
message.setTitle("에러내용");
message.setValue(errorLog.getMessage());
message.setShorten(false);
fields.add(message);

SlackField path = new SlackField();
path.setTitle("요청 URL");
path.setValue(errorLog.getPath());
path.setShorten(false);
fields.add(path);

SlackField date = new SlackField();
date.setTitle("발생시간");

LocalDateTime localDateTime = LocalDateTime.ofInstant(errorLog.getErrorDatetime(), ZoneId.of("Asia/Seoul"));
date.setValue(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
date.setShorten(true);
fields.add(date);

SlackField profile = new SlackField();
profile.setTitle("프로파일");
profile.setValue(errorLog.getPhase());
profile.setShorten(true);
fields.add(profile);

SlackField system = new SlackField();
system.setTitle("시스템명");
system.setValue(errorLog.getSystem());
system.setShorten(true);
fields.add(system);

SlackField serverName = new SlackField();
serverName.setTitle("서버명");
serverName.setValue(errorLog.getServerName());
serverName.setShorten(true);
fields.add(serverName);

SlackField hostName = new SlackField();
hostName.setTitle("호스트명");
hostName.setValue(errorLog.getHostName());
hostName.setShorten(false);
fields.add(hostName);

if (errorLog.getUserInfo() != null) {
SlackField userInformation = new SlackField();
userInformation.setTitle("사용자 정보");
userInformation.setValue(JsonUtils.toPrettyJson(errorLog.getUserInfo()));
userInformation.setShorten(false);
fields.add(userInformation);
}

String title = errorLog.getMessage();

SlackAttachment slackAttachment = new SlackAttachment();
slackAttachment.setFallback("에러발생!! 확인요망");
slackAttachment.setColor("danger");
slackAttachment.setFields(fields);
slackAttachment.setTitle(title);

if (StringUtils.isNotEmpty(axBootContextConfig.getLoggingConfig().getAdminUrl())) {
slackAttachment.setTitleLink(axBootContextConfig.getLoggingConfig().getAdminUrl());
}

slackAttachment.setText(errorLog.getTrace());

SlackMessage slackMessage = new SlackMessage("");

String channel = axBootLoggingConfig.getSlack().getChannel();

if (!axBootLoggingConfig.getSlack().getChannel().startsWith("#")) {
channel = "#" + channel;
}

slackMessage.setChannel(channel);
slackMessage.setUsername(String.format("[%s] - ErrorReportBot", errorLog.getPhase()));
slackMessage.setIcon(":exclamation:");
slackMessage.setAttachments(Collections.singletonList(slackAttachment));

slackApi.call(slackMessage);
}

private void toDatabase(AXBootErrorLog errorLog) {
errorLogService.saveLog(errorLog);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ${basePackage}.logging;


import ${basePackage}.utils.SessionUtils;
import com.chequer.axboot.core.utils.HttpUtils;
import com.chequer.axboot.core.utils.MDCUtil;
import com.chequer.axboot.core.utils.RequestUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class AXBootLogbackMdcFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (!HttpUtils.isMultipartFormData((HttpServletRequest) request)) {
RequestUtils requestWrapper = RequestUtils.of(request);

MDCUtil.setJsonValue(MDCUtil.HEADER_MAP_MDC, requestWrapper.getRequestHeaderMap());
MDCUtil.setJsonValue(MDCUtil.USER_INFO_MDC, SessionUtils.getCurrentMdcLoginUser((HttpServletRequest) request));
MDCUtil.set(MDCUtil.PARAMETER_BODY_MDC, requestWrapper.getRequestBodyJson((HttpServletRequest) request));
MDCUtil.set(MDCUtil.REQUEST_URI_MDC, requestWrapper.getRequestUri());
}

chain.doFilter(request, response);
}

@Override
public void destroy() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ${basePackage}.parameter;

import ${basePackage}.domain.user.SessionUser;
import ${basePackage}.utils.SessionUtils;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

public class SessionUserArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return SessionUser.class.isAssignableFrom(parameter.getParameterType());
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
return SessionUtils.getCurrentUser();
}
}
Loading

0 comments on commit 7ffb36b

Please sign in to comment.