Error while Logging in using Spring Security

Asked

Viewed 88 times

0

Recently I started using Spring Security to make the security of my application, the problem is that when I click on the button to log in the correct one was to take me to the application’s home page, but clicking is returning me a file . js that is in the js folder, then I click to go back to the previous page and click on login again and it takes me to the correct page, has this ever happened to you? I will add the images step by step:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I’ll enter the login code and the method that receives the Spring Security action and configuration:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org"
	xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
	xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
	layout:decorator="LayoutPadrao">
<head>
<title>LogSys</title>
</head>

<section layout:fragment="conteudo">
	<body>
		<div class="row">
			<div class="col-sm-4 col-sm-offset-4">
				<div th:if="${param.error}">
					<div class="alert alert-danger">Usuário ou Senha inválidos!</div>
				</div>

				<div th:if="${param.logout}">
					<div class="alert alert-info">Até logo!</div>
				</div>

				<div class="panel panel-info">
					<div class="panel-heading">
						<h3 class="panel-title">ENTRE</h3>
					</div>
					<div class="panel-body">
						<form th:action="@{/entrar}" method="post">
							<div class="form-group">
								<input name="username" class="form-control"
									placeholder="Usuário" />
							</div>

							<div class="form-group">
								<input type="password" name="password" class="form-control"
									placeholder="Senha" />
							</div>

							<div class="form-group">
								<button class="btn btn-primary btn-block">Entrar</button>
							</div>


						</form>
					</div>
				</div>
			</div>
		</div>
</section>

</body>
</html>

Class Configuration:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private ImplementsUserDetailService userDetailsService;

@Override
protected void configure(HttpSecurity http) throws Exception {


    http.
    authorizeRequests()
        .antMatchers(HttpMethod.GET,"/").permitAll()
        .anyRequest()
        .authenticated()
    .and()
    .formLogin()
        .loginPage("/entrar")
        .permitAll()
    .and()
    .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
        ;
}

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

}

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css/**","/fonts/**","/images/**");
}

}

Method that receives the request and returns the page:

@RequestMapping(method = RequestMethod.GET, path = "/entrar")
    public String entrar() {
        return "Entrar";
    }

1 answer

0


Friend, you have to ignore the folder where the scripts are. js, I believe this solves your problem:

@Override 
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**", "/css/**","/fonts/**","/images/**"); 
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.