是否可以使用 jsp 登录表单作为 Spring 安全性的自定义登录表单



我有一个 Spring 启动项目,所有视图都是 jsp 页面 我尝试使用 spring 安全性,但我无法将其与 jsp 表单一起使用 我搜索了类似的例子,但我没有找到关于它的东西,所以我的问题是:是否可以使用 jsp 表单作为自定义登录页面?

您可以在 Spring 安全性中创建自己的自定义登录页面

  1. 创建自定义页面。 例如 - 在具有命名登录名的视图中的 auth 文件夹中创建自定义登录名.jsp

  2. 在 Spring 安全配置文件中

    @Configuration
    public class SecurityConfiguration extends WebMvcConfigurerAdapter {
    private static final String LOGIN_URL = "/";
    private static final String LOGOUT_URL = "/logout";
    /**
    * Associate the specified URL with the view corresponding to that URL (the same as {@ code mvc: view - controller}).
    * <p>
    * In this class implementation, we only associate the login screen URL with the login screen view.
    * You won't be able to add model attributes through the controller registry. Instead, configure your InternalResourceViewResolver to expose beans to the JSP
    *(through request attributes, which is equivalent to what happens to model attributes). 
    *
    * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addViewControllers(org.springframework.web.servlet.config.annotation.ViewControllerRegistry)
    */
    @Override
    public void addViewControllers(final ViewControllerRegistry registry) {
    registry.addViewController(LOGIN_URL).setViewName("auth/login");
    }
    
    @Configuration
    @Order(SecurityProperties.BASIC_AUTH_ORDER)
    protected static class FormSecurity extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http.formLogin()
    .loginPage(LOGIN_URL);
    http.logout()
    .logoutRequestMatcher(new AntPathRequestMatcher(LOGOUT_URL))
    .logoutSuccessUrl(LOGIN_URL)
    .deleteCookies("JSESSIONID")
    .invalidateHttpSession(true)
    .clearAuthentication(true);
    }
    }
    }    
    

最新更新