| | |
| | | package com.yuanchu.limslaboratory.controller; |
| | | |
| | | |
| | | import com.yuanchu.limslaboratory.clients.UserLoginUtils; |
| | | import com.yuanchu.limslaboratory.pojo.User; |
| | | import com.yuanchu.limslaboratory.shiro.realm.ShiroRealm; |
| | | import com.yuanchu.limslaboratory.utils.SpringUtils; |
| | | import com.yuanchu.limslaboratory.vo.Result; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiImplicitParam; |
| | | import io.swagger.annotations.ApiImplicitParams; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.apache.shiro.authc.IncorrectCredentialsException; |
| | | import org.apache.shiro.authc.UnknownAccountException; |
| | | import org.apache.shiro.authc.UsernamePasswordToken; |
| | | import org.apache.shiro.subject.Subject; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | |
| | | import org.springframework.web.bind.annotation.RestController; |
| | |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/user") |
| | | @Api(tags = "用户模块") |
| | | public class UserController { |
| | | |
| | | @ApiOperation("用户登录") |
| | | @ApiImplicitParams(value = { |
| | | @ApiImplicitParam(name = "account", value = "账号", dataTypeClass = String.class, required = true), |
| | | @ApiImplicitParam(name = "password", value = "密码", dataTypeClass = String.class, required = true) |
| | | }) |
| | | @PostMapping("/login") |
| | | public Result<?> UserLogin(String account, String password){ |
| | | boolean loginSuccess = false; |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | if (!subject.isAuthenticated()) { |
| | | UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(account, password); |
| | | try { |
| | | subject.login(usernamePasswordToken); |
| | | loginSuccess = true; |
| | | } catch (UnknownAccountException e) { |
| | | return Result.fail(202, "没有找到该账号,请检查输入!"); |
| | | } catch (IncorrectCredentialsException e) { |
| | | return Result.fail(202, "密码不匹配,请检查输入!"); |
| | | } |
| | | } |
| | | if (loginSuccess) { |
| | | // 获取shiroRealm中的数据 |
| | | ShiroRealm bean = SpringUtils.getBean(ShiroRealm.class); |
| | | User user = bean.user; |
| | | user.setPassword(null); |
| | | UserLoginUtils bean1 = SpringUtils.getBean(UserLoginUtils.class); |
| | | return bean1.LoginExamine(user); |
| | | }else { |
| | | return Result.fail("登录失败"); |
| | | } |
| | | } |
| | | } |