关于sso:第三阶段-Day16-用户模块跳转-SSO单点登录-JSONPcors跨域方式-用户登录校检

1.实现用户模块跳转

1.1 需要阐明

阐明:当用户点击登录/注册按钮时 须要跳转到指定的页面中.

url地址1.:http://www.jt.com/user/regist…
url地址2: http://www.jt.com/user/login….

1.2 编辑UserController

`package com.jt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller     //须要进行页面跳转
@RequestMapping("/user")
public class UserController {

    /**
     * 实现用户模块页面跳转
     * url1: http://www.jt.com/user/login.html     页面:login.jsp
     * url2: http://www.jt.com/user/register.html  页面:register.jsp
     * 要求:实现通用页面跳转
     * restFul形式: 1.动静获取url中的参数,之后实现通用的跳转.
     */
    @RequestMapping("/{moduleName}")
    public String module(@PathVariable String moduleName){

        return moduleName;
    }

}` 

1.3 页面成果展示

2 JT-SSO我的项目创立

2.1 创立我的项目

2.2 增加继承/依赖/插件

`<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>jt-sso</artifactId>
    <!--默认的打包形式就是jar  不写也没有关系-->
    <packaging>jar</packaging>

    <parent>
        <artifactId>jt</artifactId>
        <groupId>com.jt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!--2.增加依赖信息-->
    <dependencies>
        <!--依赖本质依赖的是jar包文件-->
        <dependency>
            <groupId>com.jt</groupId>
            <artifactId>jt-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <!--3.增加插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>` 

2.3 编辑User的POJO对象

`@TableName("tb_user")
@Data
@Accessors(chain = true)
public class User extends BasePojo{

    @TableId(type = IdType.AUTO)//设定主键自增
    private Long id;            //用户ID号
    private String username;    //用户名
    private String password;    //明码 须要md5加密
    private String phone;       //电话号码
    private String email;       //临时应用电话代替邮箱

}` 


2.4 测试JT-SSO我的项目

用户通过sso.jt.com/findUserAll 获取user表中的信息 json返回
代码构造如下:

2.4.1 编辑UserController

`package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 实现测试按钮
     * 1.url地址 :findUserAll
     * 2.参数信息: null
     * 3.返回值后果: List<User>
     *
     */
    @RequestMapping("/findUserAll")
    public List<User> findUserAll(){

        return userService.findUserAll();
    }
}` 

2.4.2 编辑UserService

`package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findUserAll() {

        return userMapper.selectList(null);
    }
}` 

2.4.3 批改nginx配置

  1. 跨域实现(重要)

============

3.1 跨域拜访测试

3.1.1 同域测试

剖析:
1.浏览器地址: http://manage.jt.com/test.html
2.ajax申请地址: http://manage.jt.com/test.json
论断:
当浏览器地址与ajax申请的地址(协定://域名:端口)雷同时能够实现失常的业务调用.

`<script type="text/javascript" src="http://manage.jt.com/js/jquery-easyui-1.4.1/jquery.min.js"></script>
    <!--引入类库之后,执行js代码-->
<script type="text/javascript">
    <!--让整个页面加载实现之后执行js-->
    $(function(){
        $.get("http://manage.jt.com/test.json",function(data){
            alert(data.name);
        })
    })
</script>` 



3.1.2 跨域测试

剖析:
1.浏览器地址: http://www.jt.com/test.html
2.ajax申请地址: http://manage.jt.com/test.json
论断:
如果申请地址(协定://域名:端口)不雷同则导致申请调用失败

3.2 浏览器-同源策略阐明

阐明: 浏览器规定 发动ajax时如果申请协定/域名/端口号如果3者有一个与以后的浏览器的地址不雷同时,则违反了同源策略的规定.则浏览器不予解析返回值.

跨域问题: 违反同源策略的规定就是跨域申请.

3.3 跨域1-JSONP

3.3.1 JSONP跨域原理

  1. 利用javascrpit中的src属性实现跨域申请.
  2. 自定义回调函数 function callback(xxxx);
  3. 将返回值后果进行非凡的格局封装 callback(json);
  4. 因为利用src属性进行调用 所以只能反对get申请类型.

封装返回值:

`hello({"id":"1","name":"tom"})` 



页面js编辑

`<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试JSON跨域问题</title>    
    <script type="text/javascript">
        /*JS是解释执行的语言  */
        /*定义回调函数  */
        function hello(data){
            alert(data.name);
        }
    </script>
    <!--该json始终保留到浏览器中期待调用,然而没有函数名称无奈调用  -->
    <script type="text/javascript" src="http://manage.jt.com/test.json"></script>
    <script type="text/javascript" src="http://manage.jt.com/js/jquery-easyui-1.4.1/jquery.min.js"></script>
</head>
<body>
    <h1>JS跨域问题</h1>
</body>
</html>` 

3.3.2 JSONP

JSONP(JSON with Padding)是JSON的一种“应用模式”,可用于解决支流浏览器的跨域数据拜访的问题。因为同源策略,一般来说位于 server1.example.com 的网页无奈与不是 server1.example.com的服务器沟通,而 HTML 的

3.3.3 JSONP优化

`<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSONP测试</title>
<script type="text/javascript" src="http://manage.jt.com/js/jquery-easyui-1.4.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function(){
        alert("测试拜访开始!!!!!")
        $.ajax({
            url:"http://manage.jt.com/web/testJSONP",
            type:"get",                //jsonp只能反对get申请
            dataType:"jsonp",       //dataType示意返回值类型
            jsonp: "callback",    //指定参数名称
            jsonpCallback: "hello",  //指定回调函数名称
            success:function (data){   //data通过jQuery封装返回就是json串
                console.log(data);
            }
        });    
    })
</script>
</head>
<body>
    <h1>JSON跨域申请测试</h1>
</body>
</html>` 


3.3.4 编辑后端Controller

`package com.jt.web.controller;

import com.jt.pojo.ItemDesc;
import com.jt.util.ObjectMapperUtil;
import jdk.nashorn.internal.runtime.regexp.JoniRegExp;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class JSONPController {

    /**
     * 实现JSONP跨域申请
     * url地址: http://manage.jt.com/web/testJSONP?callback=xxxxxx
     * 参数:    临时没有能够不接
     * 返回值:  callback(JSON);
     */
     @RequestMapping("/web/testJSONP")
     public String testJSONP(String callback){
         ItemDesc itemDesc = new ItemDesc();
         itemDesc.setItemId(1000L).setItemDesc("JSONP测试!!!");
         String json = ObjectMapperUtil.toJSON(itemDesc);
         return callback+"("+json+")";
     }

}` 

3.3.5 控制台输入

3.3.6 JSONPObject阐明

 `@RequestMapping("/web/testJSONP")
    public JSONPObject testJSONP(String callback){
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(1000L).setItemDesc("JSONP测试!!!");
        return new JSONPObject(callback, itemDesc);
    }` 


3.4 cors跨域形式

3.4.1 cors调用原理

3.4.2 实现cors调用

`package com.jt.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration  //标识我是一个配置类
public class CorsConfig implements WebMvcConfigurer {

    //在后端 配置cors容许拜访的策略
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("GET","POST") //定义容许跨域的申请类型
                .allowedOrigins("*")           //任意网址都能够拜访
                .allowCredentials(true) //是否容许携带cookie
                .maxAge(1800);                 //设定申请长链接超时工夫.
    }
}` 



3.4.3 cors调用响应头解析

3.4.4 cors跨域测试


JSON数据格式

3.5 对于跨域的总结

1.jsonp
jsonp实质利用javaScript中的src属性的get申请实现的跨域.
返回值必须通过非凡的格局封装.
2.cors
增加在响应头中信息.指定哪些服务器容许拜访.

4 实现用户数据校验

4.1 业务需要

当用户在注册时,如果输出用户名,则应该向jt-sso单点登录零碎发动申请,校验用户数据是否存在.
如果存在则提醒用户.

4.2 业务接口文档阐明

4.3 前端JS剖析

1.url剖析

2.检索JS代码

  1. JS剖析
 `$.ajax({
                url : "http://sso.jt.com/user/check/"+escape(pin)+"/1?r=" + Math.random(),
                dataType : "jsonp",
                success : function(data) {
                    checkpin = data.data?"1":"0";
                    if(data.status == 200){
                         if (!data.data) {
                            validateSettings.succeed.run(option);
                            namestate = true;
                         }else {
                            validateSettings.error.run(option, "该用户名已占用!");
                            namestate = false;
                         }
                    }else{
                          validateSettings.error.run(option, "服务器正忙,请稍后!");
                          namestate = false;
                    }

                }
            });` 



4.4 编辑JT-SSO UserController

 `/**
     * 业务阐明: jt-web服务器获取jt-sso数据 JSONP跨域申请
     * url地址: http://sso.jt.com/user/check/{param}/{type}
     * 参数:    param: 须要校验的数据   type:校验的类型
     * 返回值:  SysResult对象
     * 实在的返回值: callback(SysResult的JSON)
     */
    @RequestMapping("/check/{param}/{type}")
    public JSONPObject checkUser(@PathVariable String param,
                                 @PathVariable Integer type,
                                 String callback){
        //true 示意数据存在     false 示意数据能够应用
        boolean flag = userService.checkUser(param,type);
        SysResult.success(flag);
        return new JSONPObject(callback, SysResult.success(flag));
    }` 

4.5 编辑JT-SSO UserService

 `/**
     * 判断根据:  依据用户名查问 如果后果>0 用户已存在.
     * @param param
     * @param type
     * @return
     */
    @Override
    public boolean checkUser(String param, Integer type) {
        //1.须要将type类型转化为 具体字段信息  1=username  2=phone  3=email
        String column = columnMap.get(type);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, param);
        Integer count = userMapper.selectCount(queryWrapper);
        return  count > 0 ? true :false;
    }` 


4.6 页面成果展示

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理