首先全部的请求会进过jt-web,在进行对其他系统的通信。
说明:当点击“登录”或者"免费注册"跳转到登录或者注册页面。
在jt-web项目中,声明UserController资源,完成页面资源的调度,具体代码实现如下所示:
@Controller
@RequestMapping("/user")
public class UserController {
/**
登陆页面:http://www.jt.com/user/login.html
注册页面:http://www.jt.com/user/register.html
*实现通用页面跳转
*/
@RequestMapping("/{moduleName}")
public String module(@PathVariable String moduleName) {
return moduleName;
}
}
<dependencies>
<dependency>
<groupId>com.jtgroupId>
<artifactId>jt-commonartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<configuration>
<skip>trueskip>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
在jt-common中创建pojo对象:
@TableName("tb_user")
@Data
@Accessors(chain =true)
public class User extends BasePojo{
@TableId(type =IdType.AUTO)
private Long id;
private String username;
private String password;
private String phone;
private String email;
}
public interface UserMapper extends BaseMapper<User>{
}
再复制资源,将jt-manage项目中的src/main/resources目录下的所有的资源复制到jt-sso项目下的src/main/resources目录里。然后接下来,进行资源修改。
01.将mappers目录下的ItemMapper改为UserMapper
02.将properties目录下的image.properties删掉,此模块不涉及图片上传的业务,因为不需要image.properties中的配置信息。
03.修改application.yml文件中的tomcat服务器端口号改成8093
server:
port: 8093
servlet:
context-path: /
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
#如果需要项目发布,则数据库地址必须配置远程数据库
url: jdbc:mysql://127.0.0.1:3306/jtdb?
serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true
&allowMultiQueries=true
username: root
password: root
#配置视图解析器
mvc:
view:
prefix: /WEB-INF/views/
suffix: .jsp
#mybatis-plush配置
mybatis-plus:
type-aliases-package: com.jt.pojo
mapper-locations: classpath:/mybatis/mappers/*.xml
configuration:
map-underscore-to-camel-case: true
#日志记录 输出数据库的日志信息.
logging:
config: classpath:logging-config.xml
level:
com.jt.mapper: debug
04.将jt-manage中的主启动类复制一份到jt-sso中
@SpringBootApplication
@MapperScan("com.jt.mapper")
public class SpringBootRun {
public static void main(String[] args) {
SpringApplication.run(SpringBootRun.class, args);
}
}
在jt-sso中:
UserService接口:
package com.jt.service;
public interface UserService{
}
UserServiceImpl 接口实现类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
}
UserController:
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getMsg")
public String test() {
return "单点登录项目搭建成功";
}
}
配置Nginx
#配置前台管理服务器
server {
listen 80;
server_name sso.jt.com;
location / {
#实现http请求的转发
proxy_pass http://localhost:8093;
}
}
以上都是搭建子系统,一个是前端的jt-web,一个是后台的jt-sso
$.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.data) {
validateSettings.succeed.run(option);
namestate = true;
}else {
validateSettings.error.run(option, "该用户名已占用!");
namestate = false;
}
}
});
/**
* 根据web中传递的参数,实现数据校验
* url:http://sso.jt.com/user/check/{param}/{type}
* 参数:param/type
* 返回值: SysResult对象
* JSONP跨域访问 需要数据的封装
*/
@RequestMapping("/check/{param}/{type}")
public JSONPObject checkUser(@PathVariable String param,
@PathVariable Integer type,
String callback) {
//查询后台数据库,检查数据是否存在.
Boolean flag = userService.checkUser(param,type);
//封装返回值结果
SysResult sysResult = SysResult.success(flag);
return new JSONPObject(callback, sysResult);
}
}
编辑jt-sso的UserService实现类
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMaper;
/**
* 需要根据参数,查询数据库.
* type:1 username、2 phone、3 email
* 返回值: true用户已存在,false用户不存在,
*/
@Override
public Boolean checkUser(String param, Integer type) {
//String column = type==1?"username":(type==2?"phone":"email");
Map<Integer,String> map = new HashMap<Integer, String>();
map.put(1, "username");
map.put(2, "phone");
map.put(3, "email");
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.eq(map.get(type), param);
int count = userMaper.selectCount(queryWrapper);
//返回值:true用户已存在,false用户不存在
return count==0?false:true;
}
}