创建learning-online-auth
工程,目录结构如下
learning-online-media ├── src/main/java │ ├── com.swx │ │ ├── auth │ │ │ ├── config │ │ │ └── controller │ │ ├── ucenter │ │ │ ├── client │ │ │ ├── mapper │ │ │ ├── model │ │ │ └── service │ │ └── AuthApplication.java │ └──resource └── pom.xml
|
创建工程结构
在pom文件中添加如下依赖:
pom.xml<dependencies> <dependency> <groupId>com.swx</groupId> <artifactId>learning-online-base</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency> </dependencies>
|
创建启动类,在com.swx
包下创建启动类AuthApplication
,内容如下:
AuthApplication@SpringBootApplication @EnableTransactionManagement @EnableFeignClients(basePackages = "com.swx.ucenter.client") public class AuthApplication { public static void main(String[] args) { SpringApplication.run(AuthApplication.class, args); } }
|
项目配置信息,在resources
下创建bootstrap.yml
bootstrap.ymlspring: application: name: auth-service cloud: nacos: discovery: server-addr: 124.221.23.47:8848 namespace: learning-online-dev group: learning-online-project config: server-addr: 124.221.23.47:8848 namespace: learning-online-dev group: learning-online-project file-extension: yaml refresh-enabled: true shared-configs: - data-id: logging-${spring.profiles.active}.yaml group: learning-online-common refresh: true - data-id: feign-${spring.profiles.active}.yaml group: learning-online-common refresh: true profiles: active: dev
|
日志配置,在resources
下创建log4j2-dev.xml
log4j2-dev.xml<?xml version="1.0" encoding="UTF-8"?> <Configuration monitorInterval="180" packages=""> <properties> <property name="logdir">logs</property> <property name="PATTERN">%date{YYYY-MM-dd HH:mm:ss,SSS} %level [%thread][%file:%line] - %msg%n%throwable</property> </properties> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="${PATTERN}"/> </Console>
<RollingFile name="ErrorAppender" fileName="${logdir}/error.log" filePattern="${logdir}/$${date:yyyy-MM-dd}/error.%d{yyyy-MM-dd-HH}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> </RollingFile>
<RollingFile name="DebugAppender" fileName="${logdir}/info.log" filePattern="${logdir}/$${date:yyyy-MM-dd}/info.%d{yyyy-MM-dd-HH}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> </RollingFile> <Async name="AsyncAppender" includeLocation="true"> <AppenderRef ref="ErrorAppender"/> <AppenderRef ref="DebugAppender"/> </Async> </Appenders> <Loggers>
<logger name="cn.itcast.wanxinp2p.consumer.mapper" level="DEBUG"> </logger>
<logger name="springfox" level="INFO"> </logger> <logger name="org.apache.http" level="INFO"> </logger> <logger name="com.netflix.discovery" level="INFO"> </logger> <logger name="RocketmqCommon" level="INFO" > </logger> <logger name="RocketmqRemoting" level="INFO" > </logger> <logger name="RocketmqClient" level="WARN"> </logger>
<logger name="org.dromara.hmily" level="WARN"> </logger>
<logger name="org.dromara.hmily.lottery" level="WARN"> </logger>
<logger name="org.dromara.hmily.bonuspoint" level="WARN"> </logger> <Root level="DEBUG" includeLocation="true"> <AppenderRef ref="AsyncAppender"/> <AppenderRef ref="Console"/> <AppenderRef ref="DebugAppender"/> </Root> </Loggers> </Configuration>
|
添加基础代码
实体类
使用代码生成工具生成lo_user
表的实体类,将其拷贝到learning-online-auth
工程的com.swx.ucenter.model.po
包下,暂时所需实体类如下:
- XcCompany.java
- XcCompanyUser.java
- XcMenu.java
- XcPermission.java
- XcRole.java
- XcTeacher.java
- XcUser.java
- XcUserRole.java
Mapper层
将代码生成的所有Mapper.java文件拷贝到learning-online-auth
工程下的com.swx.ucenter.mapper
包下。
将代码生成的所有Mapper.xml文件拷贝到learning-online-auth
工程下的resources/mapper
目录下。
Service层
将代码生成的所有Service和ServiceImpl文件拷贝到learning-online-auth
工程下的com.swx.ucenter.service
包下。
Controller层
在com.swx.auth.controller
包下,新建登陆Controller,添加如下的基础代码
@Api(value = "统一登陆认证接口", tags = "统一登陆认证接口") @RestController public class LoginController {
private final XcUserService xcUserService;
public LoginController(XcUserService xcUserService) { this.xcUserService = xcUserService; }
@RequestMapping("/login-success") public String loginSuccess() {
return "登陆成功"; }
@RequestMapping("/user/{id}") public XcUser getUser(@PathVariable("id") String id) { XcUser xcUser = xcUserService.getById(id); return xcUser; }
@RequestMapping("/r/r1") public String r1() { return "访问r1资源"; }
@RequestMapping("/r/r2") public String r2() { return "访问r2资源"; } }
|
配置Mybatis分页
在com.swx.auth.config
包下创建 MybatisPlusConfig
MybatisPlusConfig@Configuration @MapperScan("com.swx.ucenter.mapper") public class MybatisPlusConfig {
@Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
|