ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java
@@ -55,6 +55,23 @@ } /** * 登录方法(不需要验证码) * * @param loginBody 登录信息 * @return 结果 */ @PostMapping("/login/no") public AjaxResult login2(@RequestBody LoginBody loginBody) { // 生成令牌 String token = loginService.loginWithoutValidate(loginBody.getUsername(), loginBody.getPassword()); HashMap<String, String> tokenMap = new HashMap<>(); tokenMap.put("token",token); return AjaxResult.success(tokenMap); } /** * 获取用户信息 * * @return 用户信息 ruoyi-admin/src/main/resources/application-druid.yml
@@ -6,8 +6,8 @@ druid: # 主库数据源 master: url: jdbc:mysql://47.93.189.255:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 # url: jdbc:mysql://localhost:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 # url: jdbc:mysql://47.93.189.255:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 url: jdbc:mysql://localhost:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: ZhangApp123! # password: 123456 ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
@@ -115,6 +115,7 @@ // 静态资源,可匿名访问 .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll() .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() .antMatchers("/login/**").permitAll() // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated() .and() ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
@@ -58,6 +58,60 @@ @Autowired private ZfRegisterService registerService; public String loginWithoutValidate(String username, String password) { // 登录前置校验 loginPreCheck(username, password); // 用户验证 Authentication authentication = null; try { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); AuthenticationContextHolder.setContext(authenticationToken); // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername authentication = authenticationManager.authenticate(authenticationToken); } catch (Exception e) { if (e instanceof BadCredentialsException) { AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"))); throw new UserPasswordNotMatchException(); } else { AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage())); throw new ServiceException(e.getMessage()); } } finally { AuthenticationContextHolder.clearContext(); } AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); LoginUser loginUser = (LoginUser) authentication.getPrincipal(); recordLoginInfo(loginUser.getUserId()); SysUser sysUser = userService.selectUserByUserName(username); Long userId = sysUser.getUserId(); // 生成token String token = tokenService.createToken(loginUser); SysUserRole1 sysUserRole = registerService.selectUserRole(userId); if(sysUserRole==null){ //如果用户还没有被赋予角色,那就给一个默认是家庭成员的角色 registerService.setRoleToUser(userId, 102L); } return token; } /** * 登录验证 *