本文介绍了如何使用 Spring Security 将登录的用户重定向到他的首页,将未登录的用户重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个 Spring 网络应用程序项目作为学校的最终项目.我们将 SpringBoot 与 Hibernate 和 H2 数据库一起使用.为了进行身份验证和授权,我们使用 Spring Security.我们还将模型 View-Controller 与存储库、服务和控制器一起使用.我有一个初始首页 (带有 URL/"),在您打开项目时显示.登录后,您将被重定向到另一个带有 URL ("/portadaUsuario") 的首页(我来自西班牙,所以有很多西班牙语名字).但是,如果您在登录后以某种方式最终出现在 ("/") URL 中,您将看到与向未登录用户显示的相同的首页,其中包含注册和登录选项,即显然是错误的.

I'm doing a Spring web-application project as a final project for school. We use SpringBoot with Hibernate and an H2 database. In order to authenticate and authorize, we use Spring Security. We also use the model View-Controller with repositories, services, and controllers.I have an initial front page (with the URL "/") that is shown when you open the project. Once you log-in, you're redirected to another front page with the URL ("/portadaUsuario")(I'm from Spain so there's a lot of names in Spanish). But, if you somehow end up in the ("/") URL after you've logged in, you're shown the same front page that is shown to non-logged users with the sign-up and log-in options, which is obviously wrong.

我知道我可以用 spring 安全标签和元素显示不同的 HTML 元素,但我已经围绕有两个不同的首页构建了我的项目,如果可能的话,我想保持这种方式.如果无法实现,请告诉我应该如何在同一个 HTML 文件中显示不同的元素.

I know that I can show different HTML elements with spring security tags and elements, but I've already built my project around having two different front-pages and I would like to keep it that way if possible. If it isn't achievable, please let me know how should I proceed to show different elements in the same HTML file.

这里是我的 WellcomeController 的方法

Here are the methods of my WellcomeController

@Controller
public class PortadaController {

    @GetMapping({"/"})
    public String mostrarPortada() {
        return "portada";
    }

    @GetMapping("/portadaUser")
    public String mostrarPortadaUsuario() {
        return "/usuario/portada";
    }
}

以下是验证用户身份的方法

Here are the methods that authenticate the users

@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/css/**","/js/**","/webjars/**", "/h2-console/**", "/", "/newUser/**").permitAll()
                        .antMatchers("/admin/**").hasAnyRole("ADMIN")
                        .anyRequest().authenticated()
                        .and()
                .formLogin()
                        .loginPage("/login")
                        .permitAll()
                        .successHandler(customSuccessHandler)
                        .defaultSuccessUrl("/portadaUser")
                        .and()
                .logout()
                        .logoutUrl("/logout")
                        .permitAll()
                        .logoutSuccessUrl("/")
                        .and()
                .exceptionHandling()
                        .accessDeniedPage("/acceso-denegado");


        http.csrf().disable();
        http.headers().frameOptions().disable();
        }

我想要的是应用程序检测登录用户何时请求/"URL,并将其重定向到 /portadaUser.

What I want is the application detecting when a logged user is requesting the "/" URL, and for it to be redirected to /portadaUser instead.

推荐答案

假设你有两页,一个用于登录(signed-in-home),另一个用于未登录(not-signed-in-home),你的代码是这样的 -

Assuming, you have two pages, one for signed in (signed-in-home) and another for not signed in(not-signed-in-home), you code something like this -

@GetMapping("/")
public String index(Principal principal, Model model) {
   return principal != null ? "signed-in-home" : "not-signed-in-home";
}

在此处阅读有关主体的更多信息 - https://www.baeldung.com/get-user-in-spring-security

Read more about principal here- https://www.baeldung.com/get-user-in-spring-security

这篇关于如何使用 Spring Security 将登录的用户重定向到他的首页,将未登录的用户重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 10:42