Я настроил все параметры для своего весеннего проекта, но когда я пытаюсь войти в приложение, для каждого запроса говорится, что
"The server understood the request but refuses to authorize it."
Первоначально я пытался реализовать аутентификацию JDBC (вы можете видеть, что я использую источник данных в своем коде). Но затем я попытался использовать аутентификацию в памяти, в обоих случаях я не могу получить доступ к ресурсам.
Ниже мой весенний конфигурационный файл,
package com.nobalg.config;
import java.beans.PropertyVetoException;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.nobalg")
@PropertySource("classpath:persistence-mysql.properties")
public class AppConfig {
@Autowired
private Environment env;
private Logger logger = Logger.getLogger(getClass().getName());
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
return resolver;
}
@Bean
public DataSource secureDataSource(){
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
//Datasource
dataSource.setDriverClass(env.getProperty("jdbc.driver"));
dataSource.setJdbcUrl(env.getProperty("jdbc.url"));
dataSource.setUser(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.password"));
//Connection polling
dataSource.setInitialPoolSize(Integer.parseInt(env.getProperty("connection.pool.initialPoolSize")));
dataSource.setMaxPoolSize(Integer.parseInt(env.getProperty("connection.pool.maxPoolSize")));
dataSource.setMinPoolSize(Integer.parseInt(env.getProperty("connection.pool.minPoolSize")));
dataSource.setMaxIdleTime(Integer.parseInt(env.getProperty("connection.pool.maxIdleTime")));
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
return dataSource;
}
}
Файл инициализатора сервлета диспетчера
package com.nobalg.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MvcSpringInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class[]{AppConfig.class};
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[]{"/"};
}
}
Файл конфигурации безопасности Spring:
package com.nobalg.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
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 SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//auth.jdbcAuthentication().dataSource(dataSource);
auth.inMemoryAuthentication().withUser("Nobal").password("test@123").authorities("MANAGER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/loginPage")
.loginProcessingUrl("/loginProcessing")
.usernameParameter("username")
.passwordParameter("password")
.permitAll();
}
}
Файл инициализатора безопасности Spring
package com.nobalg.config;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
}
Единственный и неповторимый контроллер
package com.nobalg.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class MainContoller {
@GetMapping("/loginPage")
public String showLoginForm(){
return "login";
}
}
и страница входа в систему
<%@ page language = "java" contentType = "text/html; charset=ISO-8859-1"
pageEncoding = "ISO-8859-1"%>
<%@ taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form method = "POST" action = "${pageContext.request.contextPath}/loginProcessing">
<p>Enter Username : <input type = "text" placeholder = "Enter Username" name = "username"></p>
<p>Enter Password : <input type = "password" placeholder = "Enter Password" name = "password"></p>
<p><input type = "submit" value = "LOG IN"></p>
</form:form>
</body>
</html>




Добавьте это как поле формы:
<input type = "hidden" name = "${_csrf.parameterName}" value = "${_csrf.token}"/>
Или, если вам нужен другой подход с Библиотека тегов JSP Spring Security:
При желании вы можете отключить csrf, который включен по умолчанию:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
Добавьте этот bean-компонент с помощью passwordEncoder.
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
И установите кодировщик паролей на auth:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.passwordEncoder(this.passwordEncoder());
}
Измените .loginProcessingUrl("/loginProcessing"), которому нужен UserDetailsService, на .defaultSuccessUrl("/")
В вашей форме нет включенного токена csrf.
Пожалуйста, посмотрите это
Пожалуйста, попробуйте на время отключить csrf, протестируйте его и дайте мне знать. Я тогда постараюсь выяснить, что происходит.
В нем говорится, что внутренняя ошибка, кодировщик пароля не сопоставлен, для идентификатора null
Вы можете использовать "{noop} yourpassword" вместо кодировщика паролей.
Позвольте нам продолжить обсуждение в чате.
Спасибо за жизненно важную помощь, но сейчас это странная проблема. Теперь иногда при щелчке входа в систему он обходит даже неправильные пароли имени пользователя.
Спасибо за ваш ответ, но я уже использую тег Spring <form: form>. Разве он не делает то же, что вы предложили?