2017-09-03 16 views
0

私はJavaとSpring Bootについてはかなり新しいです。私はさまざまな機能とパラメータを試してみるようになりました。Java Spring Boot - パスワードを忘れました - 速度の電子メールサービス?

  1. パスワードを忘れた - メール送信。

    //__ send email notification 
        SimpleEmailService.generateEmail("forgotPasswordTemplate", data); 
    

これは、古い電子メールサービスがどのようなものであったかである - Javaの春ブーツとequivlantどうなりますか? - インポートするライブラリは何ですか?/APIは、getサービスだろう - アーキテクチャの観点からして - 私は基本的にreactjsは、APIを使ってビルド電源にしたい -

package controller; 

import org.apache.velocity.Template; 
import org.apache.velocity.VelocityContext; 
import org.apache.velocity.app.VelocityEngine; 
import org.springframework.mail.javamail.JavaMailSender; 
import org.springframework.mail.javamail.MimeMessageHelper; 
import org.springframework.mail.javamail.MimeMessagePreparator; 

import com.mongodb.BasicDBObject; 

import javax.mail.internet.MimeMessage; 

import java.io.StringWriter; 

public class SimpleEmailService { 
    private static JavaMailSender mailSender; 
    private static VelocityEngine velocityEngine; 

    public void setMailSender(JavaMailSender mailSender) { 
     this.mailSender = mailSender; 
    } 

    public void setVelocityEngine(VelocityEngine velocityEngine) { 
     this.velocityEngine = velocityEngine; 
    } 

    public static void generateEmail(final String templateType, final BasicDBObject user) { 
     MimeMessagePreparator preparator = new MimeMessagePreparator() { 
     public void prepare(MimeMessage mimeMessage) throws Exception { 
      String velocityTemplateName = null; 
      String subject = null; 

      if(templateType.equals("forgotPasswordTemplate")){ 
       velocityTemplateName = "forgot_password"; 
       subject = "Forgot Password"; 
       user.put("imgPath", "http://livesite.com/images/emails/forgot_password/"); 
      } 


      if(templateType.equals("registerTemplate")){ 
       velocityTemplateName = "registration_confirmation"; 
       subject = "Registration"; 
       user.put("imgPath", "http://livesite.com/images/emails/registration_confirmation/"); 
      } 


      MimeMessageHelper message = new MimeMessageHelper(mimeMessage); 
       message.setTo("[email protected]"); 
       message.setFrom("[email protected]"); // could be parameterized... 


       message.setSubject(subject); 

      VelocityContext model = new VelocityContext(); 
       model.put("user", user);    

      String text = applyTemplate(model, velocityTemplateName+".vm"); 
       message.setText(text, true); 
     } 
     }; 
     mailSender.send(preparator); 
    } 

    public static String applyTemplate(VelocityContext data, String templateName){     
     StringWriter sw= new StringWriter();   
     Template t = velocityEngine.getTemplate("net/velocity/templates/"+templateName); 
     t.merge(data, sw); 
     return sw.toString();       
    } 
} 

は、これが私の現在のコードベースは、次のようになります - - 他はフォームからの投稿です。

- 春ブーツ速度をサポートするために特別に

package controller; 


import java.util.List; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpSession; 

import org.apache.tomcat.util.log.SystemLogHandler; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.CrossOrigin; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.RestController; 

import domain.Account; 
import repo.AccountRepository; 


@RestController 
public class MyService { 

    @Autowired 
    private AccountRepository accountRepository; 


    @RequestMapping(value = "/api", method = RequestMethod.GET) 
    @CrossOrigin(origins = {"*"}) 
    public ResponseEntity<?> index(@RequestParam(value="name", required=false, defaultValue="admin1") String name) throws Exception { 
      Account acc = accountRepository.findByUsername(name); 
      return new ResponseEntity<>(acc, HttpStatus.OK); 
    } 




    @RequestMapping(value = "/login", method = RequestMethod.GET) 
    @CrossOrigin(origins = {"*"}) 
    public ResponseEntity<?> login(
      @RequestParam(value="email", required=false, defaultValue="email") String email, 
      @RequestParam(value="password", required=false, defaultValue="password") String password, 
      HttpServletRequest request 
      ) throws Exception { 
      List<Account> acc = accountRepository.findByEmail(email); 

      HttpSession session = request.getSession(true); 
      session.setAttribute("user", acc); 

      System.out.println("logged>>>"+acc); 
      System.out.println("session>>>"+session); 

      return new ResponseEntity<>(acc, HttpStatus.OK); 
    } 

    @SuppressWarnings("unchecked") 
    @RequestMapping(value = "/logout", method = RequestMethod.GET) 
    @CrossOrigin(origins = {"*"}) 
    public ResponseEntity<?> logout(
      HttpServletRequest request 
      ) throws Exception { 

      HttpSession session = request.getSession(true); 
      List<Account> acc = (List<Account>) session.getAttribute("user"); 

      session.removeAttribute("user");    

      System.out.println("logout>>>"+acc); 
      System.out.println("session>>>"+session); 

      return new ResponseEntity<>(acc, HttpStatus.OK); 
    } 

    @RequestMapping(value = "/forgot", method = RequestMethod.GET) 
    @CrossOrigin(origins = {"*"}) 
    public ResponseEntity<?> forgot(
      @RequestParam(value="email", required=false, defaultValue="email") String email 
      ) throws Exception { 
      List<Account> acc = accountRepository.findByEmail(email); 
      return new ResponseEntity<>(acc, HttpStatus.OK); 
    } 

    @RequestMapping(value = "/edit", method = RequestMethod.GET) 
    @CrossOrigin(origins = {"*"}) 
    public ResponseEntity<?> edit(
      @RequestParam(value="username", required=false, defaultValue="username") String username, 
      @RequestParam(value="password", required=false, defaultValue="password") String password, 
      @RequestParam(value="email", required=false, defaultValue="email") String email 
      ) throws Exception { 
      Account acc = accountRepository.findByUsername(username); 
      return new ResponseEntity<>(acc, HttpStatus.OK); 
    } 

    @RequestMapping(value = "/register", method = RequestMethod.GET) 
    @CrossOrigin(origins = {"*"}) 
    public ResponseEntity<?> register(
      @RequestParam(value="username", required=false, defaultValue="username") String username, 
      @RequestParam(value="password", required=false, defaultValue="password") String password, 
      @RequestParam(value="email", required=false, defaultValue="email") String email 
      ) throws Exception { 
     Account checkAccount = accountRepository.findByUsername(username); 
     System.out.println("checkAccount>>>"+checkAccount); 
     if (checkAccount == null) { 
      try {    
       // if not create some 
       Account newAcc = accountRepository.saveAndFlush(new Account(
          username, 
          password, 
          email 
         )); 
       System.out.println("newAcc>>>"+checkAccount); 
       return new ResponseEntity<>(newAcc, HttpStatus.OK); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
       System.out.println("error adding user>>>"+checkAccount); 
       return new ResponseEntity<>(e, HttpStatus.OK); 
      } 
     }else{ 
      // String e = "user exists"; 
      System.out.println("user already exists>>>"+checkAccount); 
      return new ResponseEntity<>(checkAccount, HttpStatus.OK); 
     } 
    } 

} 
+0

私はこのチュートリアルがhttps://www.quickprogrammingtips.com/spring-boot/how-to-send-email-from-spring-boot-applications.htmlに役立つと思います。また、このリファレンスを読むことができますhttps:// docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/htmlsingle/#mail https://docs.spring.io/spring-boot/docs/current/reference/html/boot- features-email.html –

+0

これは似ています - 一つの例では "freemarker templates"と他の "velocity" - 任意の環境設定を使用していますか?私はベロシティテンプレートを使用する古いコードを持っています –

+0

この記事を見て、両方のテンプレートエンジンのドキュメントを見直して、あなたの要件を満たすものを選択するのに役立ちます。https://github.com/spring-projects/spring-boot/commit/86d87f6b8ad6816b1d98bad7ab68bcdad1bf5cf0 –

答えて

関連する問題