单点登录失败解决措施 单点登录框架有哪些( 二 )


package com.jt.auth.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.authority.AuthorityUtils;import org.springframework.security.core.userdetails.User;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.core.userdetails.UsernameNotFoundException;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.stereotype.Service;import java.util.List;/** * 登录时用户信息的获取和封装会在此对象进行实现, * 在页面上点击登录按钮时,会调用这个对象的loadUserByUsername方法, * 页面上输入的用户名会传给这个方法的参数 */@Servicepublic class UserDetailsServiceImpl implements UserDetailsService {@Autowiredprivate BCryptPasswordEncoder passwordEncoder;//UserDetails用户封装用户信息(认证和权限信息)@Overridepublic UserDetails loadUserByUsername(String username)throws UsernameNotFoundException {//1.基于用户名查询用户信息(用户名,用户状态,密码,....)//Userinfo userinfo=userMapper.selectUserByUsername(username);String encodedPassword=passwordEncoder.encode("123456");//2.查询用户权限信息(后面会访问数据库)//这里先给几个假数据List<GrantedAuthority> authorities =AuthorityUtils.createAuthorityList(//这里的权限信息先这么写,后面讲"sys:res:create", "sys:res:retrieve");//3.对用户信息进行封装return new User(username,encodedPassword,authorities);}}网关中登陆路由配置在网关配置文件中添加登录路由配置,例如
- id: router02uri: lb://sca-auth#lb表示负载均衡,底层默认使用ribbon实现predicates: #定义请求规则(请求需要按照此规则设计)- Path=/auth/login/** #请求路径设计filters:- StripPrefix=1 #转发之前去掉path中第一层路径基于Postman进行访问测试启动sca-gateway,sca-auth服务,然后基于postman访问网关,执行登录测试,例如:
自定义登陆页面在sca-resource-ui工程的static目录中定义登陆页面,例如:
<!doctype html><html lang="en"><head><!-- Required meta tags --><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><!-- Bootstrap CSS --><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"><title>login</title></head><body><div class="container"id="app"><h3>Please Login</h3><form><div class="mb-3"><label for="usernameId" class="form-label">Username</label><input type="text" v-model="username" class="form-control" id="usernameId" aria-describedby="emailHelp"></div><div class="mb-3"><label for="passwordId" class="form-label">Password</label><input type="password" v-model="password" class="form-control" id="passwordId"></div><button type="button" @click="doLogin()" class="btn btn-primary">Submit</button></form></div><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script><script src="https://www.520longzhigu.com/uploads/allimg/220429/091205HL-3.jpg"></script><script>var vm=new Vue({el:"#app",//定义监控点,vue底层会基于此监控点在内存中构建dom树data:{ //此对象中定义页面上要操作的数据username:"",password:""},methods: {//此位置定义所有业务事件处理函数doLogin() {//1.定义urllet url = "http://localhost:9000/auth/login"//2.定义参数let params = new URLSearchParams()params.append('username',this.username);params.append('password',this.password);//3.发送异步请求axios.post(url, params).then((response) => {debuggerlet result=response.data;console.log(result);if (result.state == 200) {alert("login ok");} else {alert(result.message);}})}}});</script></body></html>启动sca-resource-ui服务后,进入登陆页面,输入用户名jack,密码123456进行登陆测试 。


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: