Spring Security Login System更新用户详细信息

时间:2020-02-13 19:28:20

标签: java spring spring-mvc spring-security basic-authentication

我正在尝试使用Spring Security创建一个登录系统,该系统可以在登录后更改用户名和密码。它仅需要一个用户,因此我试图不必使用数据库。该代码似乎可以编译并正确运行,但是用户名和密码未更新。我是春天的新手,所以我可能缺少一些简单的东西。感谢您的任何答复。

安全配置-Login.java

package project.BenH;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration//sets this as a configuration file
@EnableWebSecurity//enables Spring's security functions
public class Login extends WebSecurityConfigurerAdapter {
    private int count=1;
    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        String user3 = "user";
        String pass3 = "pass";
        //imports the value of user1 from LoginGreet when run after the first time
        if (count>1){
            LoginGreet loginGreet2 = new LoginGreet();
            user3 = loginGreet2.getUser1();
            pass3 = loginGreet2.getPass1();
        }
        count++;
        UserDetails user =
             User.withDefaultPasswordEncoder()//defines the username, password and role for a single user and stores them in memory
                .username(user3)
                .password(pass3)
                .roles("USER")
                .build();

        return new InMemoryUserDetailsManager(user);
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()//configures which pages are secured and which can be accessed without a password
                .antMatchers("/", "/home", "/result").permitAll()//sets these pages to be unsecured
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")//sets the login page
                .permitAll()
                .and()
            .logout()//allows all to logout 
                .permitAll();
    }

}

MVC配置单元和控制器

package project.BenH;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import project.BenH.LoginGreet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.ui.Model;

@Controller
@Configuration//sets this as a configuration file
public class MVCConfiguration implements WebMvcConfigurer {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");//creates 5 view controllers to identify and group all of the pages to allow them to be secured
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/greeting").setViewName("greeting1");
        registry.addViewController("/result").setViewName("result1");
    }
    //controller to send the values from greeting.html to LoginGreet.java
    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("loginGreet", new LoginGreet());
        return "greeting";
    }
    //controller to send the values from LoginGreet.java to result.html
    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute LoginGreet loginGreet) {
        Login login = new Login();
        login.userDetailsService();
        return "result";
    }
}

LoginGreet.java

package project.BenH;

public class LoginGreet {

    //defines two variables which act as fields in the loginGreet view
    private String user1;

    private String pass1;

    //getter for user1 for the communication of user1 between classes/templates
    public String getUser1() {
        return user1;
    }
    //setter for user1 for the communication of user1 between classes/templates
    public void setUser1(String user1) {
        this.user1 = user1;
    }

    public String getPass1() {
        return pass1;
    }

    public void setPass1(String pass1) {
        this.pass1 = pass1;
    }

}

0 个答案:

没有答案