ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 성공적인 로그인 페이지는 여전히 스프링 보안에서 금지 된 403을 표시합니다. [중복]
    카테고리 없음 2020. 8. 17. 02:31

    질문

    그래서 ive는 내 웹사이트 에서이 기본 스프링 보안을 구현했습니다. private 경로에서 올바른 자격 증명으로 로그인하면 여전히 403 금지로 표시됩니다.

    잘못된 자격 증명을 입력하면 기본 스프링 보안 로그인 페이지에 잘못된 자격 증명이 표시되므로 올바른 자격 증명을 사용하고 있습니다.

    SpringSecurityConfig

    package com.naha.crimereportingsystem;
    
    import org.springframework.beans.factory.annotation.Autowired;
    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.core.userdetails.UserDetailsService;
    import org.springframework.security.crypto.password.NoOpPasswordEncoder;
    import org.springframework.security.crypto.password.PasswordEncoder;
    
    @EnableWebSecurity
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Autowired
        UserDetailsService userDetailsService;
    
        // Authentication
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(userDetailsService);
        }
    
        // Authorization
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                .antMatchers("/admin").hasRole("ADMIN")
                .antMatchers("/police").hasRole("POLICE")
                .antMatchers("/users")
                .hasRole("USER").antMatchers("/").permitAll()
                .and()
                .formLogin().and().httpBasic();
    
            http.logout();
    
            http.csrf().disable();
        }
    
        @Bean
        public PasswordEncoder getPasswordEncoder() {
            return NoOpPasswordEncoder.getInstance();
        }
    }

    사용자 모델

    package com.naha.crimereportingsystem.user;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
        private String name;
        private String username;
        private String password;
        private boolean active = true;
        private String roles = "USER";
    
        public String getUsername() {
            return username;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getRoles() {
            return roles;
        }
    
        public void setRoles(String roles) {
            this.roles = roles;
        }
    
        public boolean isActive() {
            return active;
        }
    
        public void setActive(boolean active) {
            this.active = active;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
    }

    Spring JPA를 사용하는 메신저로 사용되는 UserRepository

    package com.naha.crimereportingsystem.user;
    
    import java.util.Optional;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface UserRepository extends JpaRepository<User, Integer> {
        Optional<User> findByUsername(String username);
    }

    MyUserDetails

    package com.naha.crimereportingsystem.user;
    
    import org.springframework.security.core.GrantedAuthority;
    import org.springframework.security.core.authority.SimpleGrantedAuthority;
    import org.springframework.security.core.userdetails.UserDetails;
    
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class MyUserDetails implements UserDetails {
    
        private String userName;
        private String password;
        private boolean active;
        private List<GrantedAuthority> authorities;
    
        public MyUserDetails(User user) {
            this.userName = user.getUsername();
            this.password = user.getPassword();
            this.active = user.isActive();
            this.authorities = Arrays.stream(user.getRoles().split(",")).map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());
        }
    
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return authorities;
        }
    
        @Override
        public String getPassword() {
            return password;
        }
    
        @Override
        public String getUsername() {
            return userName;
        }
    
        @Override
        public boolean isAccountNonExpired() {
            return true;
        }
    
        @Override
        public boolean isAccountNonLocked() {
            return true;
        }
    
        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }
    
        @Override
        public boolean isEnabled() {
            return active;
        }
    }

    MyUserDetailsSerivce

    package com.naha.crimereportingsystem.user;
    
    import org.springframework.security.core.GrantedAuthority;
    import org.springframework.security.core.authority.SimpleGrantedAuthority;
    import org.springframework.security.core.userdetails.UserDetails;
    
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class MyUserDetails implements UserDetails {
    
        private String userName;
        private String password;
        private boolean active;
        private List<GrantedAuthority> authorities;
    
        public MyUserDetails(User user) {
            this.userName = user.getUsername();
            this.password = user.getPassword();
            this.active = user.isActive();
            this.authorities = Arrays.stream(user.getRoles().split(",")).map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());
        }
    
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return authorities;
        }
    
        @Override
        public String getPassword() {
            return password;
        }
    
        @Override
        public String getUsername() {
            return userName;
        }
    
        @Override
        public boolean isAccountNonExpired() {
            return true;
        }
    
        @Override
        public boolean isAccountNonLocked() {
            return true;
        }
    
        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }
    
        @Override
        public boolean isEnabled() {
            return active;
        }
    }

    사용자 컨트롤러

    @GetMapping("/users")
        public String UserRoute() {
            return "users";
        }

    마지막으로 데이터베이스 DB 이미지


    답변1

    Userclass에서 다음 줄을 바꿉니다.

    private String roles = "USER";

    와 함께 :

    private String roles = "ROLE_USER";


     

     

     

     

    출처 : https://stackoverflow.com/questions/62682225/upon-successful-login-page-is-still-showing-403-forbidden-in-spring-security

    댓글

Designed by Tistory.