Не отображается стандартная форма логина, Spring Security
Проблема заключается в том, что при подключении Spring Security и настройке конфига все запросы так и остались незащищенными, что странно. Не могу понять, проблема в том, что чего-то не доинтегрировал? В логгере никаких проблем не обнаружено с security
Сам SecurityConfig:
@Configuration
@EnableWebSecurity
@Order(1000)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
final
DataSource dataSource;
public SecurityConfig(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin").hasRole("ADMIN")
.antMatchers("/project").hasAnyRole("ADMIN", "USER")
.antMatchers("/**").permitAll()
.and()
.formLogin();
}
@Bean
public PasswordEncoder getPasswordEncoder()
{
return NoOpPasswordEncoder.getInstance();
}
}
Также SpringConfig:
@Configuration
@ComponentScan("ru.ahpg.spring")
@EnableWebMvc
public class SpringConfig implements WebMvcConfigurer {
private final ApplicationContext applicationContext;
@Autowired
public SpringConfig(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("WEB-INF/views/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Bean
public SpringSecurityDialect springSecurityDialect() {
return new SpringSecurityDialect();
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(
"/img/**",
"/css/**",
"/libs/**",
"/js/**",
"/webjars/**")
.addResourceLocations(
"classpath:/static/img/",
"classpath:/static/css/",
"classpath:/static/libs/",
"classpath:/static/js/",
"/webjars/");
}
@Bean
public HikariConfig hikariConfig() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("org.postgresql.Driver");
hikariConfig.setJdbcUrl("jdbc:postgresql://localhost:5432/data_db");
hikariConfig.setUsername("postgres");
hikariConfig.setPassword("pass");
return hikariConfig;
}
}
и pom.xml:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${ss.ver}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${ss.ver}</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${ss.ver}</version>
</dependency>
Простая форма для логина(просто чтоб увидеть, отрабатывает ли security)
<head>
</head>
<body>
<h1>Login</h1>
<form name='f' action="login" method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='username' value=''></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' /></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
Ответы (1 шт):
Автор решения: Roman C
→ Ссылка
Форму логина точно не доконфигурировал.
На форму логина надо добавить адрес и разрешения
.formLogin().loginPage("/login").permitAll()
Чтобы запросы были защищёнными надо добавить
.anyRequest().authenticated()