package model;

 

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.Arrays;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.stereotype.Service;

 

@Service("customerService")

public class CustomerService {

    @Autowired

    private CustomerDAO customerDao;

    private MessageDigest messageDigest;

 

    public CustomerService() {

 

    }

 

    public CustomerService(CustomerDAO customerDao) {

       this.customerDao = customerDao;

       try {

           this.messageDigest = MessageDigest.getInstance("MD5");

       } catch (NoSuchAlgorithmException e) {

           e.printStackTrace();

       }

 

    }

 

    public static void main(String[] args) {

       ApplicationContext context = new ClassPathXmlApplicationContext(

              "beans.config.xml");

 

       CustomerService customerService = (CustomerService) context

              .getBean("customerService");

 

       CustomerBean bean = customerService.login("Babe", "B");

       System.out.println(bean);

 

       customerService.changPassword("Ellen", "E", "ABC");

 

       ((ConfigurableApplicationContext) context).close();

    }

 

    public boolean changPassword(String username, String oldPassword,

           String newPassword) {

       CustomerBean bean = this.login(username, oldPassword);

       if (bean != null) {

           byte[] temp = newPassword.getBytes(); 

           temp=messageDigest.digest(temp);//使用者輸入:亂碼

           return customerDao.update(temp, bean.getEmail(), bean.getBirth(),

                  username);

       }

       return false;

    }

 

    public CustomerBean login(String username, String password) {

       CustomerBean bean = customerDao.select(username);

       if (bean != null) {

           if (password != null && password.length() != 0) {

              byte[] pass = bean.getPassword(); //資料庫抓出:亂碼

              byte[] temp = password.getBytes(); //使用者輸入:明碼

              temp = messageDigest.digest(temp);     //使用者輸入:亂碼

              if (Arrays.equals(pass, temp)) {

                  return bean;

              }

           }

       }

       return null;

    }

}




package misc;

 

import java.io.IOException;

 

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.annotation.WebFilter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

 

import model.CustomerBean;

 

@WebFilter("/pages/*")

public class PageFilter implements Filter {

    private FilterConfig fConfig;

 

    public PageFilter() {

    }

 

    public void destroy() {

    }

 

    public void doFilter(ServletRequest req, ServletResponse resp,

           FilterChain chain) throws IOException, ServletException {

       HttpServletRequest   request = (HttpServletRequest) req;

       HttpServletResponse response=(HttpServletResponse)resp;

       HttpSession sessionrequest.getSession();

       CustomerBean bean=(CustomerBean) session.getAttribute("user");

       if(bean!=null){

           chain.doFilter(request, response);

       }else{

           String uri = request.getRequestURI();

           session.setAttribute("dest", uri);

           String path=request.getContextPath();

           response.sendRedirect(path+"/secure/login.jsp");

       }

    }

 

    public void init(FilterConfig fConfig) throws ServletException {

       this.fConfig = fConfig;

    }

 

}

 

創作者介紹

學習筆記專區

乙方 發表在 痞客邦 PIXNET 留言(0) 人氣()