Как реализовать безопасность в весеннем приложении? - PullRequest
0 голосов
/ 28 апреля 2020

Я создаю весеннее приложение для проекта колледжа.

package com.sales.security;

import org.springframework.context.annotation.Bean;
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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{ 
 @Override
 protected void configure(HttpSecurity http) throws Exception {
  http
    .authorizeRequests()
    .antMatchers("/showProducts.html", "/showOrders.html", "/showCustomers.html","/newOrder.html","/addProduct.html","/addCustomer.html")
    .authenticated()
    .and()
    .formLogin();
  }

 private static final String ENCODED_PASSWORD = "$2y$12$i4Cl5SZgrPFItSz/G5cvTObf0sqzHszwwKMZ4pQeUlElY1BR7KxdO"; //password is "user" encrypted using BCrypt


 @Override
 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
     auth.inMemoryAuthentication()
         .passwordEncoder(passwordEncoder())
         .withUser("user").password(ENCODED_PASSWORD).roles("USER");
 }


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

Я взял код от пользователя TwiN по адресу Java Spring Security - User.withDefaultPasswordEncoder () устарел?

Я изменил га sh быть "пользователем" и подтвердил, что он определенно "пользователь", используя https://bcrypt-generator.com/

, но, несмотря ни на что, страница входа не позволит мне войти и говорит что мои данные для входа неверны вот как выглядит мое приложение после ввода username = "user" и password = "user"

Ответы [ 2 ]

0 голосов
/ 28 апреля 2020

Добавьте следующие свойства в application.properties

spring.security.user.name = user
spring.security.user.password = user
spring.security.user.roles = USER

ИЛИ

Добавьте их к текущему SecurityConfig class

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
0 голосов
/ 28 апреля 2020

есть много способов реализовать безопасность весной, я не могу следить за тем, что вы делаете, но вот мой репозиторий github Spring Boot Security вы можете изменить ветви, чтобы увидеть разные способы применения безопасности, выберите ветвь "в памяти", ее самая простая.

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