【问题标题】:Spring, @RolesAllowed and database to secure pagesSpring,@RolesAllowed 和数据库来保护页面
【发布时间】:2015-03-31 04:20:08
【问题描述】:

我的数据库中有两个表格。用户和用户角色。

CREATE TABLE `user` (
  `username` varchar(50) NOT NULL,
  `password` varchar(255) NOT NULL,
  `enable` tinyint(4) NOT NULL DEFAULT '1',
  PRIMARY KEY (`username`),
  UNIQUE KEY `unique_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `user_roles` (
  `user_role_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(45) NOT NULL,
  `ROLE` varchar(45) NOT NULL,
  PRIMARY KEY (`user_role_id`),
  UNIQUE KEY `uni_username_role` (`ROLE`,`username`),
  KEY `fk_username_idx` (`username`),
  CONSTRAINT `fk_username` FOREIGN KEY (`username`) REFERENCES `user` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

user_roles 中的“ROLE”显示用户拥有的角色{ROLE_USER, ROLE_ADMIN}

我想使用@RolesAllowed 拒绝用户访问某些页面(并授予管理员访问权限),但我不知道如何从数据库中获取 user_role 并将其发送到 RolesAllowed。

在 Controller 中获取 user_role 没有问题,但我认为在每个函数中检查用户角色并不是一个好主意。

或者,也许有比使用@RolesAllowed 更好的解决方案?

抱歉这个愚蠢的问题,这是我第一次看到 Spring 的 5 个小时。

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    您没有详细介绍您正在构建的应用程序的架构,但作为初学者,我可以提供一些我目前正在构建的应用程序的示例。我正在使用 Spring Boot、JPA、Spring Data 和 Spring Security。我有类似的要求并像这样解决了它:

    我已经实现了 UserDetailsS​​ervice 接口。它用于检索有关尝试登录的用户的信息。当我使用 JPA 和 Spring Data 时,服务和模型类看起来像这样(为简洁起见,删除了 getter、setter 和大多数字段):

    @Entity
    // in your case this would map to the User table
    public class Profile implements UserDetails {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
    
        // you should probably use bean validation / jpa to assure uniqueness etc.
        private String name;
        ...
    
        @ElementCollection(fetch = FetchType.EAGER)
        private Set<Role> roles = ImmutableSet.<Role> of(new Role("USER"));
        ...
    }
    
    @Embeddable
    // in your case this would map to the user_role table
    public class Role implements GrantedAuthority {
    
        public final static Role USER = new Role("USER");
        public final static Role ADMIN = new Role("ADMIN");
    
        private String authority;
    
        ...
    
    }
    
    @Transactional
    @Service
    public class ProfileService implements UserDetailsService {
    
        private final ProfileRepository profileRepository;
    
        @Autowired
        public ProfileService(ProfileRepository profileRepository){
                this.profileRepository = profileRepository;
        }
    
        public Profile loadUserByUsername(String username) throws UsernameNotFoundException {
            Profile profile = profileRepository.findByUsername(username);
    
            // this is the only way to authenticate
            if (profile == null) {
                throw new UsernameNotFoundException("security.userNotFound");
            }
    
            return profile;
        }
    
    // you may want to add profile creation etc.
    ...
    }
    

    设置完成后,我必须配置 Spring Security 才能使用此服务。我主要使用 Java Config,所以配置看起来像。

    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Autowired
        private ProfileService profileService;
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
            auth.userDetailsService(profileService).passwordEncoder(passwordEncoder());
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .and()
                    .authorizeRequests()
                    .antMatchers(HttpMethod.POST, "/**")
                    .authenticated()
                    ...
                    // you may want to put more config here
          }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-20
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 2014-10-10
      • 2012-11-06
      相关资源
      最近更新 更多