博客系统后台框架搭建及登录注销

博客系统后台框架搭建及登录注销_第1张图片

application.yml

server:
  port:8080
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/wzsxy?useSSL=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 123456
  thymeleaf:
    mode: HTML
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

login

<form class="ui large form" method="post" action="#" th:action="@{/admin/login}">
  <div class="ui  segment">
    <div class="field">
      <div class="ui left icon input">
        <i class="user icon"></i>
        <input type="text" name="username" placeholder="用户名">
      </div>
    </div>
    <div class="field">
      <div class="ui left icon input">
        <i class="lock icon"></i>
        <input type="password" name="password" placeholder="密码">
      </div>
    </div>
    <button class="ui fluid large teal submit button">登   录</button>
  </div>

  <div class="ui error mini message"></div>
  <div class="ui mini negative message" th:unless="${#strings.isEmpty(message)}" th:text="${message}">11</div>
</form>

LoginController

@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private IUserService userService;

    @GetMapping
    public String toLogin(){
//        ModelAndView mv=new ModelAndView();
//        mv.setViewName("admin/login");
//        return mv;
        return "admin/login";
    }

    @PostMapping("/login")
    public String login(String username, String password, HttpSession session, RedirectAttributes redirectAttributes){
        User user=userService.checkUser(username,password);
        if(user!=null){
            session.setAttribute("user",user);
            return "admin/index";
        }else{
            redirectAttributes.addFlashAttribute("message","用户名或密码错误");
            return "redirect:/admin";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "admin/login";
    }
}

UserServiceImpl

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User checkUser(String username, String password){
        return userDao.findByUsernameAndPassword(username, MD5Util.code(password));
    }
}

IUserservice

public interface IUserService {
    User checkUser(String username,String password);
}

UserDao

public interface UserDao extends JpaRepository<User,Long> {
    User findByUsernameAndPassword(String username,String password);
}

加密

/**
 * MD5加密类
 * @param str 要加密的字符串
 * @return    加密后的字符串
 */
public static String code(String str){
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[]byteDigest = md.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < byteDigest.length; offset++) {
            i = byteDigest[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        //32位加密
        return buf.toString();
        // 16位的加密
        //return buf.toString().substring(8, 24);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

你可能感兴趣的:(博客系统后台框架搭建及登录注销)