Source Code Cross Referenced for TestJAASUserRealm.java in  » Sevlet-Container » jetty-modules » org » mortbay » jetty » plus » jaas » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Sevlet Container » jetty modules » org.mortbay.jetty.plus.jaas 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // ========================================================================
002:        // $Id: TestJAASUserRealm.java 778 2006-08-15 08:31:20Z janb $
003:        // Copyright 2003-2004 Mort Bay Consulting Pty. Ltd.
004:        // ------------------------------------------------------------------------
005:        // Licensed under the Apache License, Version 2.0 (the "License");
006:        // you may not use this file except in compliance with the License.
007:        // You may obtain a copy of the License at 
008:        // http://www.apache.org/licenses/LICENSE-2.0
009:        // Unless required by applicable law or agreed to in writing, software
010:        // distributed under the License is distributed on an "AS IS" BASIS,
011:        // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012:        // See the License for the specific language governing permissions and
013:        // limitations under the License.
014:        // ========================================================================
015:
016:        package org.mortbay.jetty.plus.jaas;
017:
018:        import java.io.BufferedReader;
019:        import java.io.File;
020:        import java.io.FileOutputStream;
021:        import java.io.FileReader;
022:        import java.io.FileWriter;
023:        import java.io.PrintWriter;
024:        import java.sql.Connection;
025:        import java.sql.PreparedStatement;
026:        import java.sql.Statement;
027:        import java.util.Properties;
028:        import java.util.Random;
029:
030:        import javax.naming.Context;
031:        import javax.naming.InitialContext;
032:
033:        import junit.framework.Test;
034:        import junit.framework.TestCase;
035:        import junit.framework.TestSuite;
036:
037:        import org.apache.derby.jdbc.EmbeddedDataSource;
038:        import org.mortbay.jetty.Request;
039:
040:        /* ---------------------------------------------------- */
041:        /** TestJAASUserRealm
042:         * <p> Test JAAS in Jetty - relies on the JDBCUserRealm.
043:         *
044:         * <p><h4>Notes</h4>
045:         * <p>
046:         *
047:         * <p><h4>Usage</h4>
048:         * <pre>
049:         */
050:        /*
051:         * </pre>
052:         *
053:         * @see
054:         * @version 1.0 Mon Apr 28 2003
055:         * @author Jan Bartel (janb)
056:         */
057:        public class TestJAASUserRealm extends TestCase
058:
059:        {
060:            private static boolean setupDone = false;
061:            private Random random = new Random();
062:
063:            public TestJAASUserRealm(String name) throws Exception {
064:                super (name);
065:
066:            }
067:
068:            public static Test suite() {
069:                return new TestSuite(TestJAASUserRealm.class);
070:            }
071:
072:            public void setUp() throws Exception {
073:                if (setupDone)
074:                    return;
075:
076:                //set up the properties 
077:                File propsFile = File.createTempFile("props", null);
078:                propsFile.deleteOnExit();
079:                Properties props = new Properties();
080:                props.put("user", "user,user,pleb");
081:                FileOutputStream fout = new FileOutputStream(propsFile);
082:                props.store(fout, "");
083:                fout.close();
084:
085:                //set up config
086:                File configFile = File.createTempFile("loginConf", null);
087:                configFile.deleteOnExit();
088:                PrintWriter writer = new PrintWriter(new FileWriter(configFile));
089:                writer.println("props {");
090:                writer
091:                        .println("org.mortbay.jetty.plus.jaas.spi.PropertyFileLoginModule required");
092:                writer.println("debug=\"true\"");
093:                writer.println("file=\""
094:                        + propsFile.getCanonicalPath().replace('\\', '/')
095:                        + "\";");
096:                writer.println("};");
097:                writer.println("ds {");
098:                writer
099:                        .println("org.mortbay.jetty.plus.jaas.spi.DataSourceLoginModule required");
100:                writer.println("debug=\"true\"");
101:                writer.println("dbJNDIName=\"ds\"");
102:                writer.println("userTable=\"myusers\"");
103:                writer.println("userField=\"myuser\"");
104:                writer.println("credentialField=\"mypassword\"");
105:                writer.println("userRoleTable=\"myuserroles\"");
106:                writer.println("userRoleUserField=\"myuser\"");
107:                writer.println("userRoleRoleField=\"myrole\";");
108:                writer.println("};");
109:                writer.flush();
110:                writer.close();
111:
112:                BufferedReader reader = new BufferedReader(new FileReader(
113:                        configFile));
114:                String s = "";
115:                for (s = reader.readLine(); (s != null); s = reader.readLine()) {
116:                    System.out.println(s);
117:                }
118:
119:                //create a login module config file
120:                System.setProperty("java.security.auth.login.config",
121:                        configFile.toURL().toExternalForm());
122:                setupDone = true;
123:            }
124:
125:            public void testItDataSource() throws Exception {
126:                String tmpDir = System.getProperty("java.io.tmpdir")
127:                        + System.getProperty("file.separator");
128:                System.setProperty("derby.system.home", tmpDir);
129:                String dbname = "derby-" + (int) (random.nextDouble() * 10000);
130:
131:                EmbeddedDataSource eds = new EmbeddedDataSource();
132:
133:                try {
134:                    //make the java:comp/env
135:                    InitialContext ic = new InitialContext();
136:                    Context comp = (Context) ic.lookup("java:comp");
137:                    Context env = comp.createSubcontext("env");
138:
139:                    //make a DataSource    
140:                    eds.setDatabaseName(dbname);
141:                    eds.setCreateDatabase("create");
142:
143:                    env.createSubcontext("jdbc");
144:                    env.bind("ds", eds);
145:
146:                    Connection connection = eds.getConnection();
147:
148:                    //create tables
149:                    String sql = "create table myusers (myuser varchar(32) PRIMARY KEY, mypassword varchar(32))";
150:                    Statement createStatement = connection.createStatement();
151:                    createStatement.executeUpdate(sql);
152:
153:                    sql = " create table myuserroles (myuser varchar(32), myrole varchar(32))";
154:                    createStatement.executeUpdate(sql);
155:                    createStatement.close();
156:
157:                    //insert test users and roles
158:                    sql = "insert into myusers (myuser, mypassword) values (?, ?)";
159:
160:                    PreparedStatement statement = connection
161:                            .prepareStatement(sql);
162:                    statement.setString(1, "me");
163:                    statement.setString(2, "me");
164:
165:                    statement.executeUpdate();
166:                    sql = "insert into myuserroles (myuser, myrole) values ( ? , ? )";
167:                    statement = connection.prepareStatement(sql);
168:                    statement.setString(1, "me");
169:                    statement.setString(2, "roleA");
170:                    statement.executeUpdate();
171:
172:                    statement.setString(1, "me");
173:                    statement.setString(2, "roleB");
174:                    statement.executeUpdate();
175:
176:                    statement.close();
177:                    connection.close();
178:
179:                    //create a JAASUserRealm
180:                    JAASUserRealm realm = new JAASUserRealm("testRealm");
181:
182:                    realm.setLoginModuleName("ds");
183:
184:                    JAASUserPrincipal userPrincipal = (JAASUserPrincipal) realm
185:                            .authenticate("me", "blah", (Request) null);
186:                    assertNull(userPrincipal);
187:
188:                    userPrincipal = (JAASUserPrincipal) realm.authenticate(
189:                            "me", "me", (Request) null);
190:
191:                    assertNotNull(userPrincipal);
192:                    assertNotNull(userPrincipal.getName());
193:                    assertTrue(userPrincipal.getName().equals("me"));
194:
195:                    assertTrue(userPrincipal.isUserInRole("roleA"));
196:                    assertTrue(userPrincipal.isUserInRole("roleB"));
197:                    assertTrue(!userPrincipal.isUserInRole("roleC"));
198:
199:                    realm.pushRole(userPrincipal, "roleC");
200:                    assertTrue(userPrincipal.isUserInRole("roleC"));
201:                    assertTrue(!userPrincipal.isUserInRole("roleA"));
202:                    assertTrue(!userPrincipal.isUserInRole("roleB"));
203:
204:                    realm.pushRole(userPrincipal, "roleD");
205:                    assertTrue(userPrincipal.isUserInRole("roleD"));
206:                    assertTrue(!userPrincipal.isUserInRole("roleC"));
207:                    assertTrue(!userPrincipal.isUserInRole("roleA"));
208:                    assertTrue(!userPrincipal.isUserInRole("roleB"));
209:
210:                    realm.popRole(userPrincipal);
211:                    assertTrue(userPrincipal.isUserInRole("roleC"));
212:                    assertTrue(!userPrincipal.isUserInRole("roleA"));
213:                    assertTrue(!userPrincipal.isUserInRole("roleB"));
214:
215:                    realm.popRole(userPrincipal);
216:                    assertTrue(!userPrincipal.isUserInRole("roleC"));
217:                    assertTrue(userPrincipal.isUserInRole("roleA"));
218:
219:                    realm.disassociate(userPrincipal);
220:                } finally {
221:                    try {
222:                        Connection c = eds.getConnection();
223:                        Statement s = c.createStatement();
224:                        s.executeUpdate("drop table myusers");
225:                        s.executeUpdate("drop table myuserroles");
226:                        s.close();
227:                        c.close();
228:                    } catch (Exception e) {
229:                        e.printStackTrace();
230:                    }
231:                }
232:            }
233:
234:            public void testItPropertyFile() throws Exception {
235:                //create a JAASUserRealm
236:                JAASUserRealm realm = new JAASUserRealm("props");
237:                realm.setLoginModuleName("props");
238:
239:                JAASUserPrincipal userPrincipal = (JAASUserPrincipal) realm
240:                        .authenticate("user", "wrong", (Request) null);
241:                assertNull(userPrincipal);
242:
243:                userPrincipal = (JAASUserPrincipal) realm.authenticate("user",
244:                        "user", (Request) null);
245:
246:                assertNotNull(userPrincipal);
247:                assertTrue(userPrincipal.getName().equals("user"));
248:
249:                assertTrue(userPrincipal.isUserInRole("pleb"));
250:                assertTrue(userPrincipal.isUserInRole("user"));
251:                assertTrue(!userPrincipal.isUserInRole("other"));
252:
253:                realm.disassociate(userPrincipal);
254:            }
255:
256:            public void tearDown() throws Exception {
257:
258:            }
259:
260:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.