Получение загрузки файла вместо перенаправления на URL по умолчанию после успешного входа - PullRequest
0 голосов
/ 16 февраля 2019

Я пытаюсь использовать Spring Security с базой данных, и, следуя примеру, я могу войти в систему (вызывается onAuthenticationSuccess), но вместо перенаправления на страницу по умолчанию я получаю пустой файл для загрузки.Я ожидаю, что меня перенаправят на страницу по умолчанию defaultSuccessUrl("/", true)

@GetMapping(path = "/")
public String displayInitialPage(Model model) {
    return "index";
}

класс конфигурации безопасности:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private WebApplicationContext applicationContext;

@Autowired
private UserService userDetailsService;

@Autowired
private DataSource dataSource;

@Autowired
private AuthenticationSuccessHandlerImpl successHandler;

@PostConstruct
public void completeSetup() {
    userDetailsService = applicationContext.getBean(UserService.class);
}

@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(encoder()).and()
            .authenticationProvider(authenticationProvider()).jdbcAuthentication().dataSource(dataSource);
}

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/register", "/style", "/script");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().authenticated().antMatchers("/login").permitAll().and().formLogin()
            .loginPage("/login").defaultSuccessUrl("/", true).permitAll().successHandler(successHandler).and().csrf().disable();
}

@Bean
public DaoAuthenticationProvider authenticationProvider() {
    final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
    authProvider.setUserDetailsService(userDetailsService);
    authProvider.setPasswordEncoder(encoder());
    return authProvider;
}

@Bean
public PasswordEncoder encoder() {
    return new BCryptPasswordEncoder(11);
}

/**
 * Enables activation of automatic resolving of spring-data specific expressions annotated on classes
 * @return SecurityEvaluationContextExtension
 */
@Bean
public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
    return new SecurityEvaluationContextExtension();
}

Ответы [ 2 ]

0 голосов
/ 23 февраля 2019

Я изменил класс конфигурации безопасности, как показано ниже:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserService userDetailsService;

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

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/register", "/style", "/script");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().authenticated().antMatchers("/login").permitAll().and().formLogin()
            .loginPage("/login").defaultSuccessUrl("/", true).permitAll();
}

@Bean
public PasswordEncoder encoder() {
    return new BCryptPasswordEncoder(11);
}
}
0 голосов
/ 18 февраля 2019

Это означает, что браузер не распознает ответ, предполагает, что это файл (в крайнем случае)

У вас есть этот контроллер:

@GetMapping(path = "/")
public String displayInitialPage(Model model) {
    return "index";
}

Итак, Spring примет значение"index" и попробуйте сопоставить это с каким-то контентом.Здесь за кулисами происходит много магии.

Допустим, вы используете Spring Boot и у вас сборка gradle, ваши зависимости:

    compile group: "org.springframework.security", name: "spring-security-core", version: "$springSecurityVersion"
    compile group: "org.springframework.security", name: "spring-security-web", version: "$springSecurityVersion"
    compile group: "org.springframework.boot", name: "spring-boot-starter-web", version: "$springBootVersion"
    compile group: "org.springframework.boot", name: "spring-boot-starter-security", version: "$springBootVersion"
    compile group: "org.springframework.boot", name: "spring-boot-starter-thymeleaf", version: "$springBootVersion"
    compile group: "org.thymeleaf.extras", name: "thymeleaf-extras-springsecurity5", version: "$thymeleafExtrasSpringSecurityVersion"

Обратите внимание на последние две строки.Они позволяют thymeleaf как шаблонизатор.Spring будет искать в каталоге

./src/main/resources/templates

файл с именем index.html

Этот файл может выглядеть следующим образом:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
    <title>Spring Security - Simple Flow for Spring Boot Authentication</title>
    <meta charset="utf-8" />
</head>
<body>
<div style="float: right" th:fragment="logout" sec:authorize="isAuthenticated()">
    <div style="float:left">
        <span style="font-weight:bold">User: </span><span sec:authentication="name"></span>
    </div>
    <div style="float:none">&nbsp;</div>
    <div style="float:right">
        <form action="#" th:action="@{/local/logout}" method="post">
            <input type="submit" value="Local Logout" />
        </form>
    </div>
</div>
<h1>Success</h1>
</body>
</html>

Внутри Spring Web MVC, тамэто bean-компонент с именем

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/templates/");
        viewResolver.setSuffix(".html");
        ....
        return viewResolver;
    }

. Spring Boot имеет компонент, называемый автоматической настройкой.Таким образом, он ищет присутствующие библиотеки и соответствующим образом настраивает распознаватели.Существуют распознаватели JSP для устаревших приложений, распознаватели сопоставления контента, когда вы хотите отправить обратно JSON, XML или другие форматы, и мой предпочтительный, thymeleaf, для отправки содержимого HTML.

В любой момент времени,Вы можете настроить несколько распознавателей.

В репозитории моего сообщества есть много примеров, с которыми можно поиграть.

https://github.com/fhanik/spring-security-community/tree/master/samples

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