简单点的tomcat带的验证授权机制,页面按钮可以通过struts的
logic:role标签控制复杂一点的是基于spring的acegi,可以实现方法级的拦截
他也有支持的jsp标签

解决方案 »

  1.   

    --参考:
    /*--软件权限设置控制的例子
    --*/CREATE TABLE 功能 (
    id int IDENTITY (1, 1),
    功能名称 nvarchar (50),
    parentid int
    )
    GOCREATE TABLE 用户 (
    id int IDENTITY (1, 1) NOT NULL,
    用户名 nvarchar (50),
    密码 nvarchar (50),
    修改日期 datetime,
    起始日期 datetime
    )
    GOCREATE TABLE 权限 (
    id int IDENTITY (1, 1) NOT NULL,
    用户ID int,
    功能id int,
    操作权限 int,
    )
    GO--数据
    --软件的功能分类
    insert into 功能
    select '产品销售',0
    union all select '电器',1
    union all select '食品类',1
    union all select '日常用品',1
    union all select '家电',2
    union all select '电脑',2--用户
    insert into 用户
    select '张三',null,getdate(),getdate()
    union all select '李四',null,getdate(),getdate()--权限分配,假设每个功能都有这样的权限设置:1.新增/2.修改/3.删除/4.打印
    insert into 权限
    select 1,2,power(2,1)+power(2,2)+power(2,4)  --张三具有电器类的1.新增/2.修改/4.打印的功能
    union all select 1,4,power(2,1)+power(2,2) --张三具有日常用品类的1.新增/2.修改的功能
    union all select 2,1,power(2,1)+power(2,2)+power(2,3)+power(2,4)  --李四具有所有权限
    /*
    select * from 权限
    drop table 权限
    比如说,我想查询张三对电器类有什么权限的时候我应该怎么写?
    谁能仔细说一下这中权限设计的原理???
    */--将邹老大的题改一下,变成查李四的,李四有所有权限,按说也应有电器类的权限
    --但结果不符
    select 新增=case power(2,1) & 操作权限 when power(2,1) then '√' else '' end
    ,修改=case power(2,2) & 操作权限 when power(2,2) then '√' else '' end
    ,删除=case power(2,3) & 操作权限 when power(2,3) then '√' else '' end
    ,打印=case power(2,4) & 操作权限 when power(2,4) then '√' else '' end
    from 权限 a
    join 用户 b on a.用户ID=b.id and b.用户名='李四' 
    join 功能 c on a.功能ID=c.id and c.功能名称='电器'
    52.--如何使用的示例:--测试数据
    create table 角色表(RoleID int,RoleName varchar(10))
    insert into 角色表
    select 1,'Admin'
    union all select 2,'cwb'
    union all select 3,'manage'create table 权限表(RightID int,RightName varchar(50))
    insert into 权限表
    select 1,'M1_Browser'
    union all select 2,'M1_Add'
    union all select 3,'M1_Modify'
    union all select 4,'M1_Delete'
    union all select 5,'M1_Print'
    union all select 6,'M2_Browser'
    union all select 7,'M2_Print'
    union all select 8,'M3_Browser'
    union all select 9,'M3_Add'
    union all select 10,'M3_Modify'
    union all select 12,'M3_Print'create table 角色权限表(RoleID int,RightID int)
    insert into 角色权限表
    select 1,1
    union all select 1,2
    union all select 1,3
    union all select 1,6
    union all select 2,6
    union all select 2,7
    go--创建显示的存储过程
    create proc p_show
    @RoleName varchar(10)
    as
    select 模块=模块
     ,browser=max(case 权限 when 'browser' then 1 else 0 end)
     ,[Add]=max(case 权限 when 'Add' then 1 else 0 end)
     ,[Modify]=max(case 权限 when 'Modify' then 1 else 0 end)
     ,[delete]=max(case 权限 when 'delete' then 1 else 0 end)
     ,[print]=max(case 权限 when 'print' then 1 else 0 end)
    from(
    select 模块=left(RightName,charindex('_',RightName)-1)
    ,权限=substring(RightName,charindex('_',RightName)+1,50)
    from 角色权限表 a
    join 权限表 b on a.RightID=b.RightID
    where RoleID=(select RoleID from 角色表 where RoleName=@RoleName)
    ) a group by 模块
    go--创建保存的存储过程
    create proc p_save
    @RoleName varchar(10), --要保存的角色名称
    @tbname sysname, --用户选择处理后的结果表名
    @deletetable bit=1 --处理完成后是否自动删除结果表,1.(默认)自动删除 0.不删除
    as
    declare @RoleID int,@s nvarchar(4000)
    select @RoleID=RoleID from 角色表 where RoleName=@RoleName
    SET XACT_ABORT ON 
    begin tran
    delete 角色权限表 where RoleID=@RoleID
    set @s='insert into 角色权限表(RoleID,RightID)
    select @RoleID,a.RightID 
    from 权限表 a join(
    select RightName=模块+''_browser'' from ['+@tbname+'] where browser=1
    union all select 模块+''_Add'' from ['+@tbname+'] where [Add]=1
    union all select 模块+''_Modify'' from ['+@tbname+'] where [Modify]=1
    union all select 模块+''_delete'' from ['+@tbname+'] where [delete]=1
    union all select 模块+''_print'' from ['+@tbname+'] where [print]=1
    ) b on a.RightName=b.RightName'
    exec sp_executesql @s,N'@RoleID int',@RoleID
    if @deletetable=1 exec('drop table ['+@tbname+']')
    commit tran
    go--创建临时表表,保存显示结果
    create table #t(模块 varchar(10),browser bit,[Add] bit,[Modify] bit,[delete] bit,[print] bit)
    insert into #t exec p_show 'admin'--修改权限
    update #t set browser=1,[Add]=1,[Modify]=1--进行权限保存
    exec p_save 'admin','#t'--显示保存结果
    exec p_show 'admin'
    go--删除测试环境
    drop table 角色表,权限表,角色权限表
    drop proc p_show,p_save