My application with Spring Security don't go beyond login page

前端 未结 1 1128
春和景丽
春和景丽 2021-01-14 21:16

I just started a project with uses Spring Security for authentication which uses Java configuration instead XML. That\'s my class SecurityConfig.java:

@Confi         


        
1条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 21:47

    Ok, I managed to solve my problem; it happens I make some mess with the Url informed in the SecurityConfig and the Url's in my views. I need remember in the future: in the class, use always //. In the view, always use .

    In my case, the views was written this way:

    index.jsp -> the login page

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
    
    
    
    
    Insert title here
    
    
    
    
    
    usuário:
    senha:

    home.jsp -> the "destiny" page (dashboard): only for test purposes in this state of project

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
    
    
    
    
    Insert title here
    
    
    
    

    ">Logout

    Final code for the class SecurityConfig.java

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth
                .inMemoryAuthentication()
                    .withUser("kleber")
                    .password("123")
                    .roles("USER");
        }
    
        protected void configure(HttpSecurity http) throws Exception {
            http
                .csrf()
                    .disable()
                .authorizeRequests()
                    .antMatchers("/css/**", "/fonts/**", "/image/**", "/js/**").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/spring/index").permitAll()
                    .loginProcessingUrl("/spring/login").permitAll()
                    .usernameParameter("login")
                    .passwordParameter("senha")
                    .successHandler(new CustomAuthenticationSuccessHandler())
                    .failureHandler(new CustomAuthenticationFailureHandler())
                    .and()
                .logout()
                    .logoutUrl("/spring/logout")
                    .logoutSuccessUrl("/spring/index").permitAll();
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题