弹簧安全中基于角色的仪表板

时间:2016-03-03 04:32:19

标签: spring spring-mvc spring-security

CREATE TABLE `role_details` (
  `role_id` int(11) NOT NULL AUTO_INCREMENT,
  `role_name` varchar(45) DEFAULT NULL,
  `role_desc` varchar(100) DEFAULT NULL,
  `rights` varchar(300) DEFAULT NULL,
  PRIMARY KEY (`role_id`)
) 



CREATE TABLE `user_details` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `display_name` varchar(45) DEFAULT NULL,
  `password` varchar(45) DEFAULT NULL,
  `emp_id` int(11) DEFAULT NULL,
  `role` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `mobile` varchar(45) DEFAULT NULL,
  `creation_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `status` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) 

这是我的数据库架构。我正在使用弹簧安全。 但我很困惑......我需要用 spring-security.xml 编写什么?

 <authentication-manager>
      <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"
          users-by-username-query=
            "SELECT * FROM hmis_db.user_details where display_name=? and status='active'"
          authorities-by-username-query=
            "**QUESTION**  " />
      </authentication-provider>
    </authentication-manager>

实际上,我想创建基于角色的仪表板。和role_details表中的'rights'指定访问菜单列表。根据它,它将生成基于角色的仪表板。

2 个答案:

答案 0 :(得分:1)

实际上我看不到你的UserDetails表和RoleDetails之间的关系,也许我正在失去一些东西。

当我制作了这样一种实体模式时,我始终在用户和角色之间建立了一对一的关系,因此一个用户可以拥有一个或多个角色。

但是假设你正在建立一个从用户到角色的1-1关系,并假设你正在寻找的ROLE_xxx位于UserDetails表的角色字段中,你的权限 - 用户名 - 查询必须或多或少像这样:

"select display_name as username, role as authority from user_details where display_name =?  " 

如果user_details中的字段角色是必须与role_details中的字段role_name匹配的外键,并且rigths实际上是security_intercept url的访问元素中引用的权限,则应该是这样的:

    "select u.display_name as username, r.rights as authority 
from user_details as u INNER JOIN role_details as r ON u.role = r.role_name  
where u.display_name =?  " 

但是,如果权限是以逗号分隔的权限列表,那么您可能会遇到麻烦。 authority-by-username-query期望接收每行中具有权限的行列表。如果这是您正在寻找的,您应该考虑更改架构或甚至查看表格,以返回每行右侧的结果

编辑:我应该这样做:

enter image description here

这是创建脚本:

CREATE TABLE IF NOT EXISTS `role_details` (
  `role_id` INT(11) NOT NULL AUTO_INCREMENT,
  `role_name` VARCHAR(45) NULL DEFAULT NULL,
  `role_desc` VARCHAR(100) NULL DEFAULT NULL,
  `role_authority` VARCHAR(300) NOT NULL,
  PRIMARY KEY (`role_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;


CREATE TABLE IF NOT EXISTS `user_details` (
  `user_id` INT(11) NOT NULL AUTO_INCREMENT,
  `display_name` VARCHAR(45) NULL DEFAULT NULL,
  `password` VARCHAR(45) NULL DEFAULT NULL,
  `emp_id` INT(11) NULL DEFAULT NULL,
  `email` VARCHAR(45) NULL DEFAULT NULL,
  `mobile` VARCHAR(45) NULL DEFAULT NULL,
  `creation_time` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
  `status` TINYINT(1) NULL DEFAULT 0,
  PRIMARY KEY (`user_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;


CREATE TABLE IF NOT EXISTS `user_role_details` (
  `user_details_user_id` INT(11) NOT NULL,
  `role_details_role_id` INT(11) NOT NULL,
  PRIMARY KEY (`user_details_user_id`, `role_details_role_id`),
  INDEX `fk_user_details_has_role_details_role_details1_idx` (`role_details_role_id` ASC),
  INDEX `fk_user_details_has_role_details_user_details_idx` (`user_details_user_id` ASC),
  CONSTRAINT `fk_user_details_has_role_details_user_details`
    FOREIGN KEY (`user_details_user_id`)
    REFERENCES `user_details` (`user_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_user_details_has_role_details_role_details1`
    FOREIGN KEY (`role_details_role_id`)
    REFERENCES `role_details` (`role_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

注意我删除了user_details中的列'role',在role_details中将'rights'重命名为'role_authority',并将'status'从varchar更改为tinyint(1)以将其用作布尔值。

然后,用户详细说明了sqls:

        users-by-username-query=
    "SELECT display_name as username, password, status as enabled
FROM user_details as u WHERE u.display_name = ? and status = 1;"

        authorities-by-username-query=
    "Select u.display_name as username, r.role_authority as authority 
        FROM 
        user_details as u 
        INNER JOIN user_role_details as urd ON u.user_id = urd.user_details_user_id 
        INNER JOIN role_details as r ON urd.role_details_role_id = r.role_id
        WHERE u.display_name = ?"

通过这种方式,您可以将多个角色绑定到每个用户

答案 1 :(得分:0)

请参阅以下链接,其中包含基本知识和示例http://en.tekstenuitleg.net/blog/spring-security-with-roles-and-rights

熟悉基础知识后,请在实施生产代码之前深入了解Spring Security文档https://docs.spring.io/spring-security/site/docs/3.0.x/reference/ns-config.html

如果您使用的是Spring Security 4,则可能更喜欢基于注释的配置,例如

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("userpwd").roles("USER");
        auth.inMemoryAuthentication().withUser("admin").password("adminpwd").roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

      http.authorizeRequests()
        .antMatchers("/", "/home").permitAll() 
        .antMatchers("/admin/**").access("hasRole('ADMIN')")
        .and().formLogin()
        .and().exceptionHandling().accessDeniedPage("/Access_Denied");

    }
}