1、登录邮件推送功能实现
This commit is contained in:
parent
0f5cf51187
commit
768dc8bc1a
|
|
@ -1,9 +1,13 @@
|
||||||
package com.xjs.business.warning;
|
package com.xjs.business.warning;
|
||||||
|
|
||||||
import com.ruoyi.common.core.constant.ServiceNameConstants;
|
import com.ruoyi.common.core.constant.ServiceNameConstants;
|
||||||
|
import com.ruoyi.common.core.domain.R;
|
||||||
|
import com.xjs.business.warning.domain.MailBean;
|
||||||
import com.xjs.business.warning.factory.RemoteMailFactory;
|
import com.xjs.business.warning.factory.RemoteMailFactory;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程调用预警服务邮件feign
|
* 远程调用预警服务邮件feign
|
||||||
|
|
@ -17,4 +21,7 @@ public interface RemoteMailFeign {
|
||||||
|
|
||||||
@GetMapping("mail/send-weather-mail")
|
@GetMapping("mail/send-weather-mail")
|
||||||
void sendWeatherMailForRPC();
|
void sendWeatherMailForRPC();
|
||||||
|
|
||||||
|
@PostMapping("mail/sendMail")
|
||||||
|
R sendMail(@RequestBody MailBean mailBean);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.xjs.business.warning.domain;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮箱发送实体
|
||||||
|
*
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-04-13
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class MailBean implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -2116367492649751917L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮件接收人
|
||||||
|
*/
|
||||||
|
private String recipient;
|
||||||
|
/**
|
||||||
|
* 邮件主题
|
||||||
|
*/
|
||||||
|
private String subject;
|
||||||
|
/**
|
||||||
|
* 邮件内容
|
||||||
|
*/
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名称
|
||||||
|
*/
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 附件地址
|
||||||
|
*/
|
||||||
|
private String absolutePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮件发送类型
|
||||||
|
*/
|
||||||
|
private MailType mailType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 内部类-邮件发送类型
|
||||||
|
*/
|
||||||
|
public enum MailType {
|
||||||
|
SIMPLE(1, "文本邮件"),
|
||||||
|
HTML(2, "HTML邮件"),
|
||||||
|
ATTACHMENT(3, "附件邮件"),
|
||||||
|
INLINE(4, "静态资源邮件"),
|
||||||
|
TEMPLATE(5, "模板邮件");
|
||||||
|
|
||||||
|
|
||||||
|
private final int code;
|
||||||
|
private final String msg;
|
||||||
|
|
||||||
|
MailType(int code, String msg) {
|
||||||
|
this.code = code;
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMsg() {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package com.xjs.business.warning.factory;
|
package com.xjs.business.warning.factory;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.domain.R;
|
||||||
import com.xjs.business.warning.RemoteMailFeign;
|
import com.xjs.business.warning.RemoteMailFeign;
|
||||||
|
import com.xjs.business.warning.domain.MailBean;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
@ -19,6 +21,12 @@ public class RemoteMailFactory implements FallbackFactory<RemoteMailFeign> {
|
||||||
@Override
|
@Override
|
||||||
public void sendWeatherMailForRPC() {
|
public void sendWeatherMailForRPC() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R sendMail(MailBean mailBean) {
|
||||||
|
log.error("远程发送邮件调用异常:"+cause.getMessage());
|
||||||
|
return R.fail("远程发送邮件调用异常:"+cause.getMessage());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,19 @@ import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
import com.ruoyi.common.security.annotation.EnableRyFeignClients;
|
import com.ruoyi.common.security.annotation.EnableRyFeignClients;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.ComponentScans;
|
||||||
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 认证授权中心
|
* 认证授权中心
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@EnableRyFeignClients
|
@EnableRyFeignClients
|
||||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
|
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
|
||||||
|
@EnableAspectJAutoProxy
|
||||||
|
@ComponentScans( {@ComponentScan("com.ruoyi"),@ComponentScan("com.xjs")} )
|
||||||
public class RuoYiAuthApplication
|
public class RuoYiAuthApplication
|
||||||
{
|
{
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.ruoyi.auth.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录切面注解
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-04-14
|
||||||
|
*/
|
||||||
|
@Target({ ElementType.PARAMETER, ElementType.METHOD })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface LoginAspect {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.ruoyi.auth.aop;
|
||||||
|
|
||||||
|
import com.ruoyi.system.api.model.LoginUser;
|
||||||
|
import com.xjs.business.warning.RemoteMailFeign;
|
||||||
|
import com.xjs.business.warning.domain.MailBean;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.AfterReturning;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录切面类
|
||||||
|
*
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-04-14
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Component
|
||||||
|
@Aspect
|
||||||
|
public class LoginAspect {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RemoteMailFeign remoteMailFeign;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 声明AOP签名
|
||||||
|
*/
|
||||||
|
@Pointcut("@annotation(com.ruoyi.auth.annotation.LoginAspect)")
|
||||||
|
public void pointcut() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理完请求后执行
|
||||||
|
*
|
||||||
|
* @param joinPoint 切点
|
||||||
|
*/
|
||||||
|
@AfterReturning(pointcut = "@annotation(loginAspect)", returning = "obj")
|
||||||
|
public void doAfterReturning(JoinPoint joinPoint, com.ruoyi.auth.annotation.LoginAspect loginAspect, Object obj) {
|
||||||
|
|
||||||
|
MailBean mailBean = new MailBean();
|
||||||
|
|
||||||
|
LoginUser loginUser = (LoginUser) obj;
|
||||||
|
mailBean.setSubject("上线提醒");
|
||||||
|
mailBean.setMailType(MailBean.MailType.HTML);
|
||||||
|
mailBean.setRecipient(loginUser.getSysUser().getEmail());
|
||||||
|
mailBean.setContent("<h3 style=\"color:red;\">" + loginUser.getSysUser().getNickName() + "上线啦</h3> " +
|
||||||
|
"<img src=\"" + loginUser.getSysUser().getAvatar() + "\" alt=\"头像\">" +
|
||||||
|
" <p>当前IP地址:" + loginUser.getSysUser().getLoginIp() + "</p>");
|
||||||
|
|
||||||
|
remoteMailFeign.sendMail(mailBean);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.ruoyi.auth.service;
|
package com.ruoyi.auth.service;
|
||||||
|
|
||||||
|
import com.ruoyi.auth.annotation.LoginAspect;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import com.ruoyi.common.core.constant.Constants;
|
import com.ruoyi.common.core.constant.Constants;
|
||||||
|
|
@ -20,7 +21,8 @@ import com.ruoyi.system.api.model.LoginUser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录校验方法<br>
|
* 登录校验方法<br>
|
||||||
* 新增功能统计用户登录次数
|
* 新增功能统计用户登录次数<br>
|
||||||
|
* 新增登录邮件推送
|
||||||
* @since 2022-01-21 11:22:16
|
* @since 2022-01-21 11:22:16
|
||||||
* @author ruoyi,xjs
|
* @author ruoyi,xjs
|
||||||
*/
|
*/
|
||||||
|
|
@ -36,6 +38,7 @@ public class SysLoginService
|
||||||
/**
|
/**
|
||||||
* 登录
|
* 登录
|
||||||
*/
|
*/
|
||||||
|
@LoginAspect
|
||||||
public LoginUser login(String username, String password)
|
public LoginUser login(String username, String password)
|
||||||
{
|
{
|
||||||
// 用户名或密码为空 错误
|
// 用户名或密码为空 错误
|
||||||
|
|
@ -141,7 +144,7 @@ public class SysLoginService
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 记录登录信息
|
* 记录登录信息
|
||||||
*
|
*
|
||||||
* @param username 用户名
|
* @param username 用户名
|
||||||
* @param status 状态
|
* @param status 状态
|
||||||
* @param message 消息内容
|
* @param message 消息内容
|
||||||
|
|
@ -164,4 +167,4 @@ public class SysLoginService
|
||||||
}
|
}
|
||||||
remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER);
|
remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
package com.xjs.controller;
|
package com.xjs.controller;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.domain.R;
|
||||||
|
import com.xjs.domain.mall.MailBean;
|
||||||
import com.xjs.service.MailService;
|
import com.xjs.service.MailService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 邮件服务控制器
|
* 邮件服务控制器
|
||||||
|
|
@ -29,4 +29,12 @@ public class MailController {
|
||||||
mailService.sendWeatherMail();
|
mailService.sendWeatherMail();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/sendMail")
|
||||||
|
@ApiOperation("发送邮件ForRPC")
|
||||||
|
public R<?> sendMail(@RequestBody MailBean mailBean) {
|
||||||
|
mailService.sendMail(mailBean);
|
||||||
|
return R.ok();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package com.xjs.service;
|
package com.xjs.service;
|
||||||
|
|
||||||
|
import com.xjs.domain.mall.MailBean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 邮件发送service接口
|
* 邮件发送service接口
|
||||||
* @author xiejs
|
* @author xiejs
|
||||||
|
|
@ -13,4 +15,10 @@ public interface MailService {
|
||||||
*/
|
*/
|
||||||
Boolean sendWeatherMail();
|
Boolean sendWeatherMail();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮件
|
||||||
|
* @param mailBean 邮件对象
|
||||||
|
*/
|
||||||
|
void sendMail(MailBean mailBean);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,5 +62,10 @@ public class MailServiceImpl implements MailService {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendMail(MailBean mailBean) {
|
||||||
|
mailServer.sendMail(mailBean);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue