springboot 同个项目两套登录系统

1.版本

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.demo</groupId>
	<artifactId>springboot-weixin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-weixin</name>
	<!-- 打包配置 -->
	<packaging>jar</packaging>
	<description>weixin project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
		<mysql-driver.version>8.0.28</mysql-driver.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<!-- 热部署 -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-devtools</artifactId>
		    <optional>true</optional>
		</dependency>
		
		<!-- thymeleaf,for html,jsp... -->
		<dependency>
		  	<groupId>org.springframework.boot</groupId>
		  	<artifactId>spring-boot-starter-thymeleaf</artifactId>
		  	<scope></scope>
		</dependency>
		
		<!-- json -->
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>fastjson</artifactId>
		    <version>1.2.62</version>
		</dependency>
		
		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
 
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		
		<!-- 数据库连接池druid -->
		<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>druid-spring-boot-starter</artifactId>
		    <version>1.1.10</version>
		</dependency>
		
		<!-- 可以使用@ConfigurationProperties -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			
			<!-- mybatis-generator.start -->
			<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId> mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql-driver.version}</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
                <configuration>
                    <!--允许移动生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 是否覆盖 -->
                    <!-- 此处要特别注意,如果不加这个设置会导致每次运行都会在原目录再次创建-->
                    <overwrite>true</overwrite>
                    <!-- generator 工具配置文件的位置 -->
                    <configurationFile>
                        src/main/resources/mybatis-generator.xml
                    </configurationFile>
                </configuration>
            </plugin>
			<!-- mybatis-generator.end -->
			
		</plugins>
	</build>

</project>

2.前后台app和web在同个项目,用包名区分后端,用文件夹名区分前端
尝试在@RequestMapping("/site")自动加上统一前缀/app或/web,避免重复写@RequestMapping("/app/site")
但是尝试失败了,设置方法可以参考
添加链接描述
因为重名问题,例如com.demo.web.controllers.SiteController和com.demo.app.controllers.SiteController,不是类名重名,而是注解的命名,例如下面代码的@RequestMapping都是同个名字/site(如果类名那里的RequestMapping不同,那即使方法那里的RequestMapping相同就没问题,估计是因为类+方法一起判断吧???)

package com.demo.web.controllers;
@Controller
@RequestMapping("/site")
public class SiteController {
}


package com.demo.app.controllers;
@Controller
@RequestMapping("/site")
public class SiteController {
}

如果都是/site就会导致启动springboot异常,即使在启动类使用FullyQualifiedAnnotationBeanNameGenerator也无法避免

@SpringBootApplication 
@ComponentScan(value = {"com.demo"}, nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)
public class SpringbootWeixinApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootWeixinApplication.class, args);
	}

}

因此只能老老实实在每个控制类加上前缀

package com.demo.web.controllers;
@Controller
@RequestMapping("/web/site")
public class SiteController {
}


package com.demo.app.controllers;
@Controller
@RequestMapping("/app/site")
public class SiteController {
}

3.前后台app和web都是同一个session,所以判断是否登录,则要用session中的不同key(可以考虑key加上前缀app_和web_区分)
设计两个拦截器AppSessionInterceptor和WebSessionInterceptor用于各自登录判断session中是否存在对应的key来判断是否登录,但是注意注册拦截器时,对于路径要判断/app和/web

package com.demo.weixin;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.demo.app.filters.AppSessionInterceptor;
import com.demo.utils.LogUtils;
import com.demo.web.filters.WebSessionInterceptor;

/**
 * 全局配置
 * @author Administrator
 */
@Configuration
public class GobalWebConfig extends WebMvcConfigurationSupport {

	/**
	 * 添加路径前缀的注解
	 * 作废!!!!因为即使前缀不同,但是值相同,例如都是/site,也会因为注解重名导致启动失败
	 * 还是老实在每个RequestMapping加上前缀吧
	 * 在控制器使用@AppRestController作为@RequestMapping替代就行
	 */
//	@Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//		//跟@RequestMapping注解的路径加上前缀/app和/web
//        configurer
//	        .addPathPrefix(urlProperties.getAppPrefix(),c -> c.isAnnotationPresent(AppController.class))
//	        .addPathPrefix(urlProperties.getAppPrefix(),c -> c.isAnnotationPresent(AppRestController.class))
//	        .addPathPrefix(urlProperties.getWebPrefix(),c -> c.isAnnotationPresent(WebController.class))
//	        .addPathPrefix(urlProperties.getWebPrefix(),c -> c.isAnnotationPresent(WebRestController.class))
//	        ;
//    }


	/**
	 * 添加静态资源映射
	 * 一定要,否则静态资源加载不了
	 */
	@Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/templates/");
//        registry.addResourceHandler("/static/img/**")
//                .addResourceLocations("file:" + "classpath:/templates/");
//        registry.addResourceHandler("/static/js/**")
//                .addResourceLocations("file:" + "classpath:/templates/");
//        registry.addResourceHandler("/static/css/**")
//                .addResourceLocations("file:" + "classpath:/templates/");
        super.addResourceHandlers(registry);
    }
	
	/**
	 * 注册拦截器
	 */
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
		LogUtils.info("全局spring配置", "加载拦截器-app登录");
		//1.app后端-登录拦截器
        //1.1注册TestInterceptor拦截器
        InterceptorRegistration registration = registry.addInterceptor(new AppSessionInterceptor());
        //1.2所有路径都被拦截
        registration.addPathPatterns("/app/**");                      
        //1.3添加不拦截路径
        //这个方式的白名单,没有再拦截器种灵活处理字符串
        registration.excludePathPatterns(    
        		//静态资源
        		"/**/*.html",            //html静态资源
        		"/**/*.js",              //js静态资源
        		"/**/*.css",             //css静态资源
        		"/**/*.woff",
        		"/**/*.ttf",
        		"/error"
         );
        LogUtils.info("全局spring配置", "加载拦截器-web登录");
		//2.web后端-登录拦截器
        registry.addInterceptor(new WebSessionInterceptor())
        	//所有路径都被拦截
        	.addPathPatterns("/web/**")
        	//添加不拦截路径
        	.excludePathPatterns(
    			//静态资源
        		"/**/*.html",            //html静态资源
        		"/**/*.js",              //js静态资源
        		"/**/*.css",             //css静态资源
        		"/**/*.woff",
        		"/**/*.ttf",
        		"/error"
        	);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值