- 浏览: 126707 次
- 性别:
- 来自: 苏州
文章分类
最新评论
-
u012883941:
Tomcat下的JAAS设置 -
u012883941:
...
Tomcat下的JAAS设置 -
yueguc:
海盗分黄金,解答
http://www.ican123.cn/ ...
一道经济学中的经典问题 -
javazhou:
java与模式 这本麽样啊
学习设计模式的重要性 -
yhbngt:
Tomcat下的JAAS设置
这次项目例子是采用struts+hibernate来搭建的,其目的是为了实现jaas在tomcate中的简化实现。在参考了很多资源后,发现利用现有框架和服务器来实现身份的验证和授权是最简单的途径,所以这次我采用的是在tomcat提供的jaas验证框架下实现jaas。虽然这个框架的功能很有限,但结合struts的授权机制就可以满足中等项目的需求了。
一
首先参考tomcat下文档的jaasRealm的配置,注意文档中的两个地方:
Quick Start
To set up Tomcat to use JAASRealm with your own JAAS login module, you will need to follow these steps:
1. Write your own LoginModule, User and Role classes based on JAAS (see the JAAS Authentication Tutorial and the JAAS Login Module Developer's Guide) to be managed by the JAAS Login Context (javax.security.auth.login.LoginContext) When developing your LoginModule, note that JAASRealm's built-in CallbackHandler +only recognizes the NameCallback and PasswordCallback at present.
2. Although not specified in JAAS, you should create seperate classes to distinguish between users and roles, extending javax.security.Principal, so that Tomcat can tell which Principals returned from your login module are users and which are roles (see org.apache.catalina.realm.JAASRealm). Regardless, the first Principal returned is always treated as the user Principal.
3. Place the compiled classes on Tomcat's classpath
4. Set up a login.config file for Java (see JAAS LoginConfig file) and tell Tomcat where to find it by specifying its location to the JVM, for instance by setting the environment variable: JAVA_OPTS=-DJAVA_OPTS=-Djava.security.auth.login.config==$CATALINA_HOME/conf/jaas.config
5. Configure your security-constraints in your web.xml for the resources you want to protect
6. Configure the JAASRealm module in your server.xml
7. Restart Tomcat 5 if it is already running.
Additional Notes
When a user attempts to access a protected resource for the first time, Tomcat 5 will call the authenticate() method of this Realm. Thus, any changes you have made in the security mechanism directly (new users, changed passwords or roles, etc.) will be immediately reflected.
Once a user has been authenticated, the user (and his or her associated roles) are cached within Tomcat for the duration of the user's login. For FORM-based authentication, that means until the session times out or is invalidated; for BASIC authentication, that means until the user closes their browser. Any changes to the security information for an already authenticated user will not be reflected until the next time that user logs on again.
As with other Realm implementations, digested passwords are supported if the <Realm> element in server.xml contains a digest attribute; JAASRealm's CallbackHandler will digest the password prior to passing it back to the LoginModule
二
按照文档Quick Start的顺序先编写jaas’s code
Role
package com.jaas;
import java.security.Principal;
/**
* Copyright (c) 2005-2007 by BenQGURU Corporation
* All rights reserved.
* @author wesley zhang(wesley.zhang@BenQ.com)
* @version $Id: Role.java,v 1.1 2007-5-11 12:36:57wesley zhang Exp $
**/
public class Role implements Principal
{
private String rolename;
public Role(String rolename)
{
this.rolename = rolename;
}
/**
* equals
*
* @param object Object
* @return boolean
* @todo Implements this java.security.Principal method
*/
public boolean equals(Object object)
{
System.out.println("object="+object.getClass().toString());
boolean flag = false;
if(object == null)
flag = false;
if(this == object)
flag = true;
if(!(object instanceof Role))
flag = false;
if(object instanceof Role)
{
Role that = (Role)object;
if(this.getName().equals(that.getName()))
{
flag = true;
}
}
System.out.println("flag="+flag);
return flag;
}
/**
* toString
* @return String
* @todo Implement this java.security.Principal method
*/
public String toString()
{
return this.getName();
}
/**
* hashCode
* @return int
* @todo Implement this java.security.Principal method
*/
public int hashCode()
{
return rolename.hashCode();
}
/**
* getName
* @return String
* @todo Implement this java.security.Principal method
*/
public String getName()
{
return this.rolename;
}
}
User
package com.jaas;
import java.security.Principal;
/**
* Copyright (c) 2005-2007 by BenQGURU Corporation
* All rights reserved.
* @author wesley zhang(wesley.zhang@BenQ.com)
* @version $Id: User.java,v 1.1 2007-5-11 12:50:36wesley zhang Exp $
**/
public class User implements Principal
{
private String username;
public User(String username)
{
this.username = username;
}
/**
* equals
* @param object Object
* @return boolean
* @todo Implement this java.serurity.Principal method
*/
public boolean equals(Object object)
{
System.out.println("object="+object.getClass().toString());
boolean flag = false;
if(object== null)
flag = false;
if(this == object)
flag = true;
if(!(object instanceof User))
flag = false;
if(object instanceof User)
{
User that = (User)object;
if(this.getName().equals(that.getName()))
{
flag = true;
}
}
System.out.println("flag="+flag);
return flag;
}
/**
* toString
* @return String
* @todo Implement this java.sercurity.Principal method
*/
public String toString()
{
return this.getName();
}
/**
* hashCode
* @return int
* @todo Implement this java.sercurity.Principal method
*/
public int hashCode()
{
return username.hashCode();
}
/**
* getName
* @return String
* @todo Implement this java.security.Principal method
*/
public String getName()
{
return this.username;
}
}
TestLoginMoudle
package com.jaas;
import java.util.Map;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
/**
* Copyright (c) 2005-2007 by BenQGURU Corporation
* All rights reserved.
* @author wesley zhang(wesley.zhang@BenQ.com)
* @version $Id: TestLoginMoudle.java,v 1.1 2007-5-11 13:01:15wesley zhang Exp $
**/
public class TestLoginMoudle implements LoginModule
{
private Subject subject;
private CallbackHandler callbackHandler;
private Map shareState;
private Map options;
//The configuration select
private boolean debug = false;
//The state
private boolean succeeded = false;
private boolean commitSucceeded = false;
private String username;
private char [] password;
//User's Principal
private User user;
private Role role;
/**
* initialize
*
* @param subject Subject
* @param callbackHandler CallbackHandler
* @param map Map
* @param map3 Map
* @todo Implement this javax.security.auth.spi.LoginMoudle method
*/
public void initialize(Subject subject, CallbackHandler callbackHandler,
Map map, Map map3)
{
// TODO Auto-generated method stub
this.subject = subject;
this.callbackHandler = callbackHandler;
this.shareState = map;
this.options = map3;
}
/**
* login
*
* @return boolean
* @throws LoginException
* @todo Implement this javax.security.auth.spi.LoginMoudle method
*/
public boolean login() throws LoginException
{
// TODO Auto-generated method stub
if(callbackHandler == null)
{
throw new LoginException("No CallBackHandler!");
}
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("user name");
callbacks[1] = new PasswordCallback("password",false);
try
{
callbackHandler.handle(callbacks);
username = ((NameCallback)callbacks[0]).getName();
password = ((PasswordCallback)callbacks[1]).getPassword();
System.out.println("username="+username);
System.out.println("password="+new String(password));
}
catch(Exception e)
{
e.printStackTrace();
}
boolean isuser = false;
boolean ispass = false;
if(username.equals("hello"))
{
isuser = true;
String pw_str = new String(password);
if(pw_str.equals("hello"))
{
System.out.println("login success!");
ispass = true;
succeeded = true;
return succeeded;
}
}
if(!isuser)
{
throw new FailedLoginException("User Name Incorrect");
}
else
{
throw new FailedLoginException("Password Incorrect!");
}
}
/**
* commit
*
* @return boolean
* @throws LoginException
* @todo Implement this javax.security.auth.spi.LoginMoudle method
*/
public boolean commit() throws LoginException
{
// TODO Auto-generated method stub
if(!succeeded)
{
return false;
}
else
{
user = new User(username);
role = new Role("guest");
if(!subject.getPrincipals().contains(user))
{
subject.getPrincipals().add(user);
}
if(!subject.getPrincipals().contains(role))
{
subject.getPrincipals().add(role);
}
if(debug)
{
System.out.println("Add subject Succeeded!");
}
username = null;
for(int i=0;i<password.length;i++)
{
password[i] = '';
}
commitSucceded = true;
return true;
}
}
/**
* abort
*
* @return boolean
* @throws loginException
* @todo Implement this javax.security.auth.spi.LoginMoudle method
*/
public boolean abort() throws LoginException
{
// TODO Auto-generated method stub
System.out.println("abort()");
if(succeeded == false)
{
return false;
}
else if(succeeded == true && commitSucceeded == false)
{
//login succed but overall authentication failed
succeeded = false;
username = null;
if(password != null)
{
for(int i = 0; i<password.length;i++)
{
&
评论
5 楼
u012883941
2015-03-07
4 楼
u012883941
2015-03-07
3 楼
yhbngt
2008-10-27
2 楼
yhbngt
2008-10-27
1 楼
天空中飞翔的鸟
2007-05-12
发表评论
-
搭建liferay-portal5.2.3的过程
2010-09-26 13:56 2106一、 运行环境 如果你只是想了解一下 lif ... -
2007年国内SaaS发展综述(转)
2008-02-28 07:57 1161软件即服务是SaaS发展的核心,你并不是真正的得到了一套物理的 ... -
JSF控件实现动态加载树状菜单
2007-09-14 20:00 4075使用JSF中的<ig:Sidebar>< ... -
Struts常见错误汇总
2007-08-14 11:24 10761、“No bean found under ... -
tomcat启动时出现的 严重: Error listenerStart (转)-
2007-08-03 15:29 1829最近看《WebWork.Spring.Hibernate整合开 ... -
用JavaServer Faces开发Web应用程序[转]
2007-06-13 19:25 1591from:http://gceclub.sun.com.cn/ ... -
jBPM开发入门指南(3)
2007-04-20 18:08 25365 安装 jBPM 的 Eclipse 开发插件 有个辅助 ... -
jBPM开发入门指南(2)
2007-04-19 18:35 21114 数据库初始化 jBPM 需要数据库支持, jBPM 会把 ... -
jBPM开发入门指南(1)
2007-04-17 18:48 2096工作流虽然还在不成熟的发展阶段,甚至还没有一个公认 ... -
jBPM应用之我见
2007-04-17 18:46 1747jBPM在2004年10月18号发布了2.0版本, ... -
JSF与Struts的异同
2007-03-31 18:09 1072Struts和JSF/Tapestry都属于表现层框架,这两 ... -
struts中使ApplicationResources.properties支持中文
2007-03-26 10:29 2894使ApplicationResources.propertie ... -
为什么要使用EJB?
2007-03-25 20:59 1104首先,我们必须明确, ... -
在Ruby on Rails/Naked Objects精神指引下的域驱动开发框架
2007-03-25 20:54 1061Ruby on Rails已经受到越来 ... -
什么是Java EE 5
2007-03-25 20:31 1371最近,SUN的伙伴们(the fo ...
相关推荐
tomcat下jaas配置实例(文档有不完善的地方) 1、需要修改 bin\startup.bat(根据自己的环境修改) SET JAVA_HOME=C:\programs\Java\jdk1.8.0_211 SET TOMCAT_HOME=C:\programs\apache-tomcat-5.5.20 2、需要修改 bin...
在Mac环境下配置Tomcat与JAAS的整合,能够为你的Web应用提供强大的安全基础。通过编写自定义的LoginModule和配置JAAS,你可以实现特定的认证需求,确保只有授权用户才能访问敏感资源。记得在部署时,要仔细检查每个...
配置可以通过代码或Jaas配置文件进行设置。 - **安全管理器(Security Manager)**:Java虚拟机中的组件,负责执行安全策略,包括控制对系统资源的访问。 **2. JAAS流程** 当应用程序启动并尝试访问受保护的资源...
JAAS配置通常位于`$JAVA_HOME/jre/lib/security/java.security`中,或在应用程序的类路径下。配置文件定义了不同的安全域(也称为"login module"),每个域有其特定的认证策略。例如: ``` jaas.myApp { ...
JAAS提供了一种标准的方式来实现这一目标,允许开发者在不深入了解底层安全机制的情况下,构建安全的应用程序。 **JAAS 登录验证机制** JAAS的核心概念是登录模块(LoginModule),它是处理用户身份验证逻辑的组件...
10. **Security**: Tomcat提供了丰富的安全功能,如角色基的安全约束、SSL/TLS支持、JAAS集成等。开发者可以使用Realm组件进行身份验证,定义AccessControlList来控制访问权限。 在学习Tomcat API时,阅读官方文档...
4. **增强的安全性**:Tomcat 7.0加强了安全特性,提供了更强的身份验证和授权机制,如集成Spring Security和JAAS,以及对SSL/TLS的改进支持,帮助保护Web应用程序免受攻击。 5. **更好的管理工具**:Tomcat 7.0...
`i`和`j-sec2`可能是文档或示例项目的部分,它们可能提供了关于如何配置和使用JAAS的详细指南,或者展示了如何将JAAS集成到具体的应用服务器如Tomcat或JBoss中的实例。 学习JAAS对于任何希望构建安全Java应用程序的...
- **Servlet容器支持**:大多数现代Servlet容器如Tomcat、Jetty都内置了对JAAS的支持,可以通过容器配置进行集成。 - **Web.xml配置**:在Web应用的部署描述符中,可以配置安全管理器,指定使用哪个JAAS Realm进行...
优化Tomcat7涉及多个方面,包括调整JVM参数、减少上下文重启、优化连接器设置、开启HTTP压缩等。理解源码有助于找到性能瓶颈并针对性地进行优化。 通过对Tomcat7源码的深入学习,开发者不仅可以了解其工作原理,还...
安装Tomcat时,用户需要根据自己的操作系统选择相应的启动脚本,并配置环境变量,例如设置`CATALINA_HOME`指向Tomcat的安装目录。对于多版本的Tomcat共存,可以通过设置不同的端口号(默认为8080)来区分不同实例。 ...
5. **安全性**:Tomcat支持多种安全机制,如SSL/TLS加密通信、JAAS认证、角色基础的访问控制等。在`server.xml`的`<Engine>`、`<Host>`或`<Context>`元素中配置这些安全设置。 6. **性能优化**:Tomcat可以通过调整...
Tomcat使用JAAS进行权限控制。默认情况下,没有预设用户。你需要编辑`conf/tomcat-users.xml`文件,添加用户并指定角色。例如,创建一个拥有`manager-gui`角色的用户,以便访问Manager App。 ```xml <tomcat-...
5. **安全性**:Tomcat提供了多种安全机制,如角色基础的访问控制(RBAC)、SSL/TLS支持以及对JAAS(Java Authentication and Authorization Service)的集成,以确保Web应用的安全性。 6. **性能优化**:Tomcat ...
4. **启动和停止服务**:通过安装目录下的bin文件夹中的`startup.bat`和`shutdown.bat`脚本启动和关闭Tomcat服务。 5. **验证安装**:在浏览器中输入`http://localhost:8080/`,如果看到Tomcat的默认欢迎页面,说明...
Tomcat还支持JAAS(Java Authentication and Authorization Service)用于身份验证和授权。 5. **性能优化**:Tomcat可以通过调整配置参数进行性能优化,例如修改最大线程数、开启连接器的NIO模式、设置连接超时等...
4. **NIO2 Connector**: Tomcat 10.0默认使用NIO2连接器,这提供了更好的并发性能和更低的内存消耗,尤其是在高并发场景下。 5. **JAR扫描优化**: 新版本对JAR扫描进行了优化,减少了启动时间和内存占用,特别是在...
5. **安全性**:Tomcat提供了多种安全机制,包括角色基础的访问控制(RBAC)、SSL/TLS支持、Jaas集成等,确保应用的安全运行。 6. **配置文件**:主要的配置文件有`server.xml`(定义服务器的全局配置)、`web.xml`...
5. **安全增强**:支持JAAS集成,增强了安全管理,如角色认证和授权。 **Tomcat8** Tomcat8于2013年发布,主要支持Java EE 7规范,包括Servlet 3.1、JSP 2.3和EL 3.0等,相比Tomcat7有以下改进: 1. **Servlet 3.1...
Tomcat支持多种安全措施,如SSL/TLS加密、角色基础的访问控制(RBAC)、JAAS认证等。在`conf/server.xml`中配置`<Realm>`元素可实现身份验证和授权。 7. **性能优化** 通过调整`conf/server.xml`中的线程池设置、...