token和session都可以用来做身份验证,而session用多了,对服务器的压力是很大的。
这里主要是生成一个uuid作为账户的身份令牌,用来校验用户是否登录。
项目里有用MyBatis-Plus,先简单说下MyBatis-Plus的配置,如果是用Mybatis之类的可以直接跳过这些配置
maven依赖相应的包
配置文件:
mybatis-plus配置
用户 实体类
用户Token
@Data 是lombok插件的注解,自动生成get、set、toString等
注意如果用了MyBatis-Plus创建的实体类必须加@TableName注解,否则会报错,当然你也得去建相应的表
配置一个自定以注解:@Login, 只要加了这个注解的就会校验用户的Token。
@LoginUser 用来获取用户信息,需要的话可以配置,也可以忽视的。
用户登录的时候给当前用户生成或修改token
controller
service
生成token createToken(long id),登出的时候调expireToken(long userId) 使当前用户的token失效时间设置为当前时间
当然还需要配置一个拦截器,拦截只要加了@Login注解的方法就会校验用户的token
拦截器:
@LoginUser 获取用户信息
WebMvcConfigurer
需要注意的是/ 不可以设置为/demo/ ,/demo 为context-path 否则会使拦截器失效
异常处理器
在swagger配置里添加securitySchemes 可以在swagger页面设置header
访问接口没有设置token的时候
获取token
将其复制粘贴到
毕业论文选题系统是基于微信小程序,后端是基于java编程语言,ssm框架,mysql数据库和IDEA工具开发,本系统分为学生,教师,管理员三个角色;学生可以注册登陆系统,查看系统公告,选课论文课题,提交课题论文,查看论文审核,答辩结果等信息;教师可以发布课题,审核学生论文,课题答辩结果,在线回复学生留言;管理员对学生,教师,公告,论文,选题,答辩等进行管理;本系统功能齐全,文档齐全,适合作为微信小程序毕业设计来参考和学习。
一技术环境
jdk版本:18 及以上
ide工具:IDEA ,微信小程序开发工具
数据库: mysql57 (必须57)
编程语言: Java
tomcat: 80 及以上
java框架:SSM
maven: 361
前端:layui ,微信
详细技术:HTML+CSS+JS+JSP+JAVA+SSM+MYSQL+JQUERY+MAVEN+微信开发工具
二项目文件(项目获取请看文末官网)
在这里插入描述
三系统功能
在这里插入描述
四代码示例
package comlmucontroller;
/
和登陆有关的都在这里
/
import comopensymphonyxwork2ActionContext;
import comopensymphonyxwork2ActionSupport;
import comlmumodelRole;
import comlmumodelUser;
import comlmuserviceRoleService;
import comlmuserviceUserService;
import comlmuutilsJsonUtils;
import comlmuutilsUserUtils;
import orgapachecommonscollectionsmapHashedMap;
import orgapachestruts2ServletActionContext;
import orgspringframeworkbeansfactoryannotationAutowired;
import orgspringframeworkcontextannotationScope;
import orgspringframeworkstereotypeController;
import javaioIOException;
import javautilHashMap;
import javautilMap;
@Controller("loginController")
@Scope("prototype")
public class LoginController extends ActionSupport {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
private User user;
private Map<String, Object> map = new HashMap();
public User getUser() {
return user;
}
public void setUser(User user) {
thisuser = user;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
thisuserService = userService;
}
/
用户登陆
@return
/
public void index() throws IOException {
User user1 = userServicegetUser(user);
if (user1 != null){
if (user1getIsSh() == 1){
if (user1getRole()getEnName()equals("admin")){
ActionContextgetContext()getSession()put("user", user1);
}
if (user1getRole()getEnName()equals("js")){
ActionContextgetContext()getSession()put("user1", user1);
}
if (user1getRole()getEnName()equals("xs")){
ActionContextgetContext()getSession()put("user2", user1);
}
mapput("flag", 1);
mapput("url", "login_indexsdo");
mapput("id", user1getId());
JsonUtilstoJson(map);
} else {
mapput("flag", 2);
JsonUtilstoJson(map);
}
} else {
mapput("flag", 3);
JsonUtilstoJson(map);
}
}
public String indexs() throws IOException {
User u = UserUtilsgetUser();
if (u != null){
ActionContextgetContext()put("user", u);
String ss = ugetRole()getEnName();
ActionContextgetContext()put("role", ugetRole()getEnName());
}
return SUCCESS;
}
//登陆页面
public String login() {
return SUCCESS;
}
//退出
public String tuichu() {
ActionContext ac = ActionContextgetContext();
Map session = acgetSession();
sessionremove("userName");
sessionremove("userId");
ServletActionContextgetRequest()getSession()invalidate();
return "login";
}
}
package comlmucontroller;
/
用户新增
/
import comopensymphonyxwork2ActionContext;
import comopensymphonyxwork2ActionSupport;
import comopensymphonyxwork2ModelDriven;
import comlmumodelRole;
import comlmumodelUser;
import comlmuserviceRoleService;
import comlmuserviceUserService;
import comlmuutilsJsonUtils;
import comlmuutilsPager;
import comlmuutilsUserUtils;
import orgspringframeworkbeansfactoryannotationAutowired;
import orgspringframeworkcontextannotationScope;
import orgspringframeworkstereotypeController;
import javaawteventFocusEvent;
import javaioIOException;
import javautilDate;
import javautilHashMap;
import javautilMap;
@Controller("userController")
@Scope("prototype")
public class UserController extends ActionSupport implements ModelDriven<User> {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
private User user;
private Integer userId;
private Map<String, Object> map = new HashMap();
/
list
@return
/
public String list() throws IOException {
User user1 = UserUtilsgetUser();
if (user1 == null || user1getId() == null){
ActionContextgetContext()put("login", 1);
return SUCCESS;
}
Pager<User> pagers = null;
Role role = user1getRole();
if (rolegetEnName()equals("admin")) {
pagers = userServicegetList(user);
ActionContextgetContext()put("pagers", pagers);
ActionContextgetContext()put("user", user1);
ActionContextgetContext()put("role", role);
ActionContextgetContext()put("bean", user);
return SUCCESS;
} else if (rolegetEnName()equals("xs") || rolegetEnName()equals("js")) {
pagers = userServicegetList(user1);
ActionContextgetContext()put("pagers", pagers);
ActionContextgetContext()put("bean", user);
return SUCCESS;
}
return null;
}
/
跳转add
@return
/
public String add() {
Pager<Role> pagers = roleServicepagers();
ActionContextgetContext()put("pagers", pagers);
return SUCCESS;
}
/
查询修改
@return
/
public String edit() {
User bean = userServicefindById(userId);
Pager<Role> pagers = roleServicepagers();
ActionContextgetContext()put("bean", bean);
ActionContextgetContext()put("pagers", pagers);
return SUCCESS;
}
/
审核
@return
/
public void updateSh() throws IOException {
usersetIsSh(1);
userServiceupdates(user);
mapput("flag", true);
mapput("url", "user_listdo");
JsonUtilstoJson(map);
}
/
更新
@return
/
public String update() throws IOException {
if (usergetPass()equals("")){
usersetPass(null);
}
userServiceupdates(user);
mapput("flag", true);
mapput("url", "user_listdo");
JsonUtilstoJson(map);
return SUCCESS;
}
/
保存
@return
/
public void save() throws IOException {
if (userServicegetUser(user) != null){
mapput("flag", false);
mapput("url", "login_logindo");
JsonUtilstoJson(map);
} else {
usersetTime(new Date());
userServicesave(user);
mapput("flag", true);
mapput("url", "login_logindo");
JsonUtilstoJson(map);
}
}
public void delete() throws IOException {
User user1 = userServicefindById(userId);
user1setIsDelete(1);
userServiceupdate(user1);
mapput("flag", true);
mapput("url", "user_listdo");
JsonUtilstoJson(map);
}
@Override
public User getModel() {
if (user == null) {
user = new User();
}
return user;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
thisuserId = userId;
}
public User getUser() {
return user;
}
public void setUser(User user) {
thisuser = user;
}
}
先判断下是否登录,然后获取当前用户对象,然后获取当前用户对象的信息:
if(is_user_logged_in()){$current_user = wp_get_current_user();
/
@example Safe usage: $current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return;
/
echo 'Username: ' $current_user->user_login '<br />';
echo 'User email: ' $current_user->user_email '<br />';
echo 'User first name: ' $current_user->user_firstname '<br />';
echo 'User last name: ' $current_user->user_lastname '<br />';
echo 'User display name: ' $current_user->display_name '<br />';
echo 'User ID: ' $current_user->ID '<br />';
}
获取服务器电脑名: PageServerManchineName获取用户信息: PageUser获取客户端电脑名:PageRequestUserHostName获取客户端电脑IP: PageRequestUserHostAddress2 在网络编程中的通用方法:获取当前电脑名: static SystemNetDnsGetHostName()根据电脑名取出全部IP地址: static SystemNetDnsResolve(电脑名)AddressList也可根据IP地址取出电脑名: static SystemNetDnsResolve(IP地址)HostName3 系统环境类的通用属性:
在微信开放平台官方公告中:
2021年4月28日24时后发布的小程序新版本,无法通过wxgetUserInfo与<button open-type="getUserInfo"/>获取用户个人信息(头像、昵称、性别与地区),将直接获取匿名数据(包括userInfo与encryptedData中的用户个人信息),获取加密后的openID与unionID数据的能力不做调整。此前发布的小程序版本不受影响,但如果要进行版本更新则需要进行适配。
新增getUserProfile接口(基础库2104版本开始支持),可获取用户头像、昵称、性别及地区信息,开发者每次通过该接口获取用户个人信息均需用户确认。具体接口文档: 《getUserProfile接口文档》
在微信开发者工具词库版本2104之后getUserInfo获取的将是微信的拟名信息。
使用uniapp官方提供的unigetUserProfile(OBJECT)接口
但是unigetUserProfile是为了支持新版微信小程序获取个人信息的接口,如官方描述一样,该接口只支持打包为微信小程序并且微信基础词库为2104以上时获取个人信息
参考: >
以上就是关于token-身份令牌的应用,判断用户是否登录全部的内容,包括:token-身份令牌的应用,判断用户是否登录、论文双选小程序怎么弄、如何获取WordPress当前用户信息等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)