@Configuration public class MyMvcConfig implements WebMvcConfigurer { //主要用于实现无业务逻辑跳转,例如主页跳转, //简单的请求重定向,错误页跳转等 @Override public void addViewControllers(ViewControllerRegistry registry) { //当访问 “/” 或 “/index.html” 时,都直接跳转到登陆页面 registry.addViewController("/").setViewName("login"); registry.addViewController("/index.html").setViewName("login"); registry.addViewController("/main.html").setViewName("main"); } //注册拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { System.out.println("MyMvcConfig.addInterceptors 注册拦截器"); registry.addInterceptor(new LoginInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/", "/login", "/index.html", "/user/login", "/css/**", "/images/**", "/js/**", "/fonts/**");//放行登录页,登陆操作,静态资源 } }
时间: 2024-04-13 14:25:18 浏览: 36
这是一个使用注解配置的Spring MVC的配置类。它实现了WebMvcConfigurer接口,并重写了其中的两个方法:addViewControllers和addInterceptors。
addViewControllers方法用于配置无业务逻辑的页面跳转。在这个例子中,当访问根路径"/"或者"/index.html"时,会直接跳转到登录页面,而访问"/main.html"时,会跳转到主页面。
addInterceptors方法用于注册拦截器。在这个例子中,注册了一个名为LoginInterceptor的拦截器。拦截器会拦截所有请求("/**"),但是排除了一些路径("/", "/login", "/index.html", "/user/login", "/css/**", "/images/**", "/js/**", "/fonts/**")。也就是说,这些路径不会被拦截器拦截,可以直接访问。
这个配置类的作用是实现简单的页面跳转和请求拦截功能。
相关问题
SpringBoot实现页面跳转重登陆
在 SpringBoot 中实现页面跳转重登录,可以通过使用 Spring Security 框架来实现。下面是实现步骤:
1. 添加 Spring Security 依赖
在 pom.xml 文件中添加 Spring Security 依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
2. 配置 Spring Security
在 SpringBoot 的主配置类中添加 @EnableWebSecurity 注解,并创建一个继承自 WebSecurityConfigurerAdapter 的配置类。通过配置类来指定安全规则和用户权限等信息。
例如,以下代码配置了一个基本的安全规则,即所有请求都需要认证,登录页面为 /login,登录失败后跳转到 /login?error 页面。
```
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").failureUrl("/login?error").permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
```
3. 创建登录页面
在 resources 目录下创建一个名为 login.html 的登录页面,用于用户输入账号和密码。
4. 创建重定向页面
在登录页面中,添加一个重定向链接,如下所示:
```
<a href="/logout?redirect=/myPage">重新登录</a>
```
其中,/logout 是 Spring Security 的默认登出链接,redirect 参数指定了登录后需要跳转的页面。
5. 创建自定义登出处理器
Spring Security 默认的登出处理器不支持重定向到指定页面,需要使用自定义登出处理器来实现。以下代码定义了一个 LogoutHandler 实现类,用于在用户登出时,重定向到指定页面。
```
@Component
public class CustomLogoutHandler implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
String redirectUrl = request.getParameter("redirect");
if (StringUtils.isBlank(redirectUrl)) {
redirectUrl = "/";
}
response.sendRedirect(redirectUrl);
}
}
```
6. 配置自定义登出处理器
在 SpringBoot 的主配置类中,通过 @Bean 注解将自定义登出处理器加入 Spring 容器,并将其设置为 Spring Security 的默认登出处理器。
```
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomLogoutHandler customLogoutHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.logout().logoutSuccessHandler(customLogoutHandler);
// 其他配置
}
// 其他配置
@Bean
public LogoutSuccessHandler logoutSuccessHandler() {
return customLogoutHandler;
}
}
```
7. 测试
完成上述步骤后,重新启动应用程序,访问 /myPage 页面。如果未登录,则会自动跳转到 /login 页面,输入正确的账号和密码后,会重定向到 /myPage 页面。当用户点击重新登录链接时,会跳转到 /login 页面,并在登录成功后重定向到 /myPage 页面。
springboot和vue路由跳转
在 Spring Boot 中,一般使用 Thymeleaf 或者其他模板引擎来生成 HTML 页面,而 Vue.js 是一种前端框架,可以实现单页面应用,可以使用 Vue Router 实现路由跳转。
对于 Spring Boot 和 Vue 路由跳转的集成,可以通过以下几步来实现:
1. 在 Spring Boot 中配置静态资源路径,可以在 `application.properties` 文件中添加以下配置:
```
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
```
2. 在 Vue 中配置路由,可以在 `src/router/index.js` 文件中添加路由配置:
```javascript
import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home.vue'
import About from '@/components/About.vue'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/about',
name: 'about',
component: About
}
]
})
```
3. 在 Spring Boot 中配置路由转发,可以在 `WebMvcConfigurer` 中添加以下配置:
```java
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// 将根路径重定向到 index.html,也就是 Vue 的入口页面
registry.addViewController("/").setViewName("forward:/index.html");
}
}
```
4. 在 Vue 中使用路由,可以在组件中添加以下代码:
```html
<template>
<div>
<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>
<router-view></router-view>
</div>
</template>
```
在这里,`<router-link>` 标签用于生成路由链接,`<router-view>` 标签用于渲染当前路由对应的组件。
通过以上步骤,就可以实现 Spring Boot 和 Vue 路由跳转的集成。需要注意的是,由于 Vue 使用的是单页面应用,所有的路由都是在前端进行处理的,因此在 Spring Boot 中需要配置路由转发,将所有请求都转发到 Vue 的入口页面上。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)