当前位置:网站首页>50: Chapter 5: develop admin management service: 3: develop [query whether the admin user name already exists, interface]; (this interface can only be called when logging in; so we have written an int
50: Chapter 5: develop admin management service: 3: develop [query whether the admin user name already exists, interface]; (this interface can only be called when logging in; so we have written an int
2022-07-25 05:28:00 【Small withered forest】
explain :
(1) The content of this blog : Development 【 Inquire about admin Does the user name already exist , Interface 】;
Catalog
One : Development 【 Inquire about admin Does the user name already exist , Interface 】;
One : Development 【 Inquire about admin Does the user name already exist , Interface 】;
0. Rationality description ;
stay 【49: The fifth chapter : Development admin management service :2: Development 【 Administrator login 】 Interface ;】 in , After the administrator user logs in , Will jump to the following page ;
then , We can set the administrator user ;
● As mentioned earlier , Duplicate login names of administrators are not allowed ; therefore , We type " Login name " When , The front-end code will capture this input action ; then , It will automatically call the corresponding interface of the back end , Check whether the login name entered has duplicate names ;
● therefore , Let's develop 【 Inquire about admin Does the user name already exist , Interface 】;
1. stay 【api】 Interface Engineering AdminMngControllerApi Interface , Definition 【 Inquire about admin Does the user name already exist , Interface 】;
/** * Inquire about admin user name , Does it already exist ; * @param username * @return */ @ApiOperation(value = " Inquire about admin user name , Does it already exist ", notes = " Inquire about admin user name , Does it already exist ", httpMethod = "POST") @PostMapping("/adminIsExist") // Set the routing , This needs to be agreed between the front and back ; public GraceJSONResult adminIsExist(@RequestParam String username);explain :
(1) This interface's url, Request mode , Parameters ; It's not nonsense , The front and rear ends need to be consistent ;
2. stay 【admin】 Managed services AdminMngController Class , To achieve 【 Inquire about admin Does the user name already exist , Interface 】;
/** * Inquire about admin user name , Does it already exist ; * @param username * @return */ @Override public GraceJSONResult adminIsExist(String username) { checkAdminExist(username);//【 Judge admin user name , Does it already exist 】 The logic of , We have drawn a single method ; return GraceJSONResult.ok(); } /** * Tool method : Judge admin user name , Does it already exist ; * @param username */ private void checkAdminExist(String username) { // 1. call Service Layer method , According to the user name , Try to check in the database , See if there is this user ; AdminUser adminUser = adminUserService.queryAdminByUsername(username); if (adminUser != null) { // If the administrator name Already exists , Directly throw a containing " The administrator login already exists !" The information of MyCustomException Custom exception ; GraceException.display(ResponseStatusEnum.ADMIN_USERNAME_EXIST_ERROR); } }explain :
(1)service Layer of 【 according to " Administrator user name ", How to query administrator users queryAdminByUsername(username);】 In the last blog , It's already developed ;
(2)【 according to " Administrator user name ", Query administrator user ;】 This logic , We have drawn a single method ;
3. test ;
(1) First, the whole situation install Look at the whole project ;
(2) then , Start the front-end project ; Remember to use SwitchHost Open virtual domain name mapping ;
(3) start-up 【admin】 The main startup class of the management service ;
……………………………………………………
(4) then , Enter a different admin user name ;
4.【 Inquire about admin Does the user name already exist , Interface 】, It also requires users to log in , Can be operated ;
PS: This is in 【36: The third chapter : Developing a pass service :19: Intercept those “ Want to visit 【 Get user account information , Interface 】 and 【 change / Improve user information , Interface 】 Request “, Check login information , Release after passing the verification ;( Use 【Spring MVC Interceptors in 】 To achieve )】、【44: Chapter four : Developing file services :5: stay 【files】 File service , Integrate FastDFS, Realization 【 Upload your avatar 】 The logic of ;】 There are settings in ;
(1) First , stay 【api】 Interface engineering , establish AdminTokenInterceptor Interceptor , To intercept requests , Judge whether the user is in login status according to the request ;
package com.imooc.api.interceptors; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * So far, : Intercept access 【 change / Improve user information , Interface 】、【 Get user account information , Interface 】, Check whether the user login status OK; */ public class AdminTokenInterceptor extends BaseInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // First , Try from the requested header in , obtain "adminUserId" and "adminUserToken"; // That is to get , Before calling 【 Inquire about admin Does the user name already exist , Interface 】 When setting into the front-end browser cookie Medium "uid" and "utoken"; // PS: When the front end initiates a request , The front-end program will put cookie in "uid" and "utoken" Set the incoming request header ; then , When setting , Their names are set to "adminUserId" and "adminUserToken"; // therefore , Don't write nonsense here , Be sure to keep consistent with the front end ; // PS: If this request is header There is no "adminUserId" and "adminUserToken", What it gets is empty ; String userId = request.getHeader("adminUserId"); String token = request.getHeader("adminUserToken"); // call BaseInterceptor Methods written in , Judge the requested header Medium "adminUserId" and "adminUserToken" whether OK; // in other words , Determine whether a front-end user is logged in ; boolean run = verifyUserIdToken(userId, token,REDIS_ADMIN_TOKEN); System.out.println(run); // If the top verifyUserIdToken() There's no problem with execution , Then the current user is logged in , // Can go to this step , direct return true, Release this request , Let it visit 【 change / Improve user information , Interface 】、【 Get user account information , Interface 】; return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }explain :
(1) How to write here , You can refer to 【36: The third chapter : Developing a pass service :19: Intercept those “ Want to visit 【 Get user account information , Interface 】 and 【 change / Improve user information , Interface 】 Request “, Check login information , Release after passing the verification ;( Use 【Spring MVC Interceptors in 】 To achieve )】;
(2) From the request header , Try to get "adminUserId" and "adminUserToken"; These two , Don't write , The front and rear ends need to be consistent ;
(3) We need to be in BaseInterceptor in , Definition REDIS_ADMIN_TOKEN;( When the user gets the administrator user login , Deposit in redis Medium token)
(2) then , stay 【api】 Interface engineering , stay InterceptorConfig class , De configuration AdminTokenInterceptor Interceptor ;
@Bean public AdminTokenInterceptor adminTokenInterceptor() { return new AdminTokenInterceptor(); } // 【 Inquire about admin Does the user name already exist , Interface 】 You need to log in to operate ; registry.addInterceptor(adminTokenInterceptor()) .addPathPatterns("/adminMng/adminIsExist");
(3) effect ;
First, the whole situation install Look at the whole project ; then , Start the front-end project ; Remember to use SwitchHost Open virtual domain name mapping ; start-up 【admin】 The main startup class of the management service ;
If , Let's get rid of cookie Information , Make it no longer login ;
边栏推荐
- Microservices and related component concepts
- VIM search and replacement and the use of regular expressions
- Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies
- odoo14 | 关于状态栏statusbar关键词使用后显示异常及解决方法
- Build keyword driven automated testing framework
- I have seven schemes to realize web real-time message push, seven!
- LCP插件创建对等VLAN接口
- The second day of rhcsa summer vacation
- Preliminary understanding of Panda3D particle system
- Uniapp custom application exit execution content
猜你喜欢

AirServer 7.3.0中文版手机设备无线传送电脑屏幕工具

Introduction to kubernetes

Three schemes for finclip to realize wechat authorized login

CSDN编程挑战赛之数组编程问题

JWT(json web token)

SystemVerilog中interface(接口)介绍

rhce第一天

Implementation principle of epoll

Delivery practice of private PAAS platform based on cloud native

Vim查找替换及正则表达式的使用
随机推荐
STL notes (VIII): container - List
H5 new feature domcontentloaded event - custom attribute -async attribute -history interface method - local storage -postmessage
弹性布局总结
Openfegin remote call lost request header problem
1310_ Implementation analysis of a printf
Redis集群搭建(Windows)
使用getifaddrs获取本机网口IP地址
systemverilog中function和task区别
LCP plug-in creates peer VLAN interface
Zhanrui's first 5g baseband chip was officially released and successfully ranked among the first tier of 5g!
The third day of rhcsa summer vacation
LCP插件创建对等VLAN接口
The price is 17300! Why does Huawei mate x face Samsung fold?
Execution process of NPM run XX
[cloud co creation] design Huawei cloud storage architecture with the youngest cloud service hcie (Part 1)
Leetcode 15: sum of three numbers
Introduction to kubernetes
Terminate 5g chip cooperation! The official response of Intel and zhanrui came
FinClip实现微信授权登录的三种方案
LCP plug-in creates peer-to-peer physical interface










