Spring Security с пользовательской формой входа не работает - PullRequest
0 голосов
/ 05 декабря 2018

У меня есть пользовательская форма входа с пружинной защитой, и при входе в систему я получаю 404

Heres my deployment structure

Вот моя веб-конфигурация

package coffee.web;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/regular/index.html");
    }

}

Вот моя конфигурация безопасности

package coffee.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("peter").password("peter").authorities("ROLE_USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/admin/**").hasRole("USER").antMatchers("/", "/**").permitAll()
        .and().formLogin().loginPage("/regular/login.html").permitAll().defaultSuccessUrl("/admin/adminIndex.html").and().csrf().disable();

    }
}

Вот моя html-форма для входа в систему

<head>
    <link href="index.css" type="text/css" rel="stylesheet" />
    <link href="adminIndex.css" type="text/css" rel="stylesheet" />
</head>

<body>
    <div id="page">
        <div id="bar"></div>
        <header>
            <h1><img id="logo" src="resources/logo.png" width="300" height="75" /></h1>
        </header>
        <section id="main">
            <div id="container">
                <div id="loginDiv">
                    <form name="login" id="login" action="login" method="POST">
                        <div id="formDiv">
                            <label for="addUsername" class="title">Username</label>
                            <span id="validateUsername" class="error"></span>
                            <input class="input" type="text" id="addUsername" name="username" />
                            <br />
                            <label for="addPassword" class="title">Password</label>
                            <span id="validatePassword" class="error"></span>
                            <input class="input" type="text" id="addPassword" name="password" />
                            <br />
                            <button id="submit" name="submit" type="submit" class="submit">submit</button>
                        </div>
                    </form>
                </div>
            </div>
        </section>
    </div>
</body>

Даже когда я отключаю авторизацию и включена только аутентификация, я все равно получаю 403код ошибки после входа в систему. Хотя я все еще мог перейти непосредственно к admin.adminIndex.html и он работает .. (с отключенной авторизацией и включенной только аутентификацией).

Хорошо, я обновил свой код безопасности с помощью "й () CSRF () отключить ();..»и теперь я получаю ошибку 404 not found, когда я вхожу в систему.

1 Ответ

0 голосов
/ 05 декабря 2018

Это случилось со мной тоже (или, по крайней мере, что-то подобное).Попробуйте добавить .and().csrf().disable() после .defaultSuccessUrl("/admin/adminIndex.html") в ваш метод настройки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...