package com.xcong.farmer.cms.common.system.controller; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.IdUtil; import com.alibaba.fastjson.JSONObject; import com.xcong.farmer.cms.common.contants.AppContants; import com.xcong.farmer.cms.common.response.Result; import com.xcong.farmer.cms.common.system.bean.LoginUserBean; import com.xcong.farmer.cms.common.system.dto.LoginDto; import com.xcong.farmer.cms.utils.RedisUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.jsoup.Jsoup; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.core.Authentication; import org.springframework.util.FileCopyUtils; import org.springframework.util.ResourceUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.io.*; import java.util.HashMap; import java.util.Map; /** * @Author wzy * @Date 2020/5/11 * @email wangdoubleone@gmail.com * @Version V1.0 **/ @Slf4j @RestController @RequestMapping(value = "/") public class LoginController { @Value("${rsa.private_key}") private String privateKey; @Resource private AuthenticationManagerBuilder authenticationManagerBuilder; @Resource private RedisUtils redisUtils; @PostMapping("/login") public Result login(@RequestBody @Validated LoginDto loginDto) { UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(loginDto.getUsername(), loginDto.getPassword()); Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authToken); String token = IdUtil.simpleUUID(); LoginUserBean loginUserBean = (LoginUserBean) authentication.getPrincipal(); redisUtils.set(AppContants.APP_LOGIN_PREFIX + token, JSONObject.toJSONString(loginUserBean), 300000); Map authInfo = new HashMap(2){ { put("token", token); put("user", loginUserBean); } }; return Result.ok("success", authInfo); } @GetMapping("/html") public Result html() throws IOException { // File file = ResourceUtils.getFile("classpath:static/index.html"); InputStream input = this.getClass().getClassLoader().getResourceAsStream("static/index.html"); File file = new File("index.html"); FileUtils.copyToFile(input, file); // ClassPathResource classPathResource = new ClassPathResource("static/index.html"); // File file = classPathResource.getFile(); return Result.ok("", Jsoup.parse(file, "utf-8").html()); } }