Advertisement
Guest User

Untitled

a guest
Dec 21st, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. [main]
  2.  
  3. sha256Matcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
  4. sha256Matcher.hashAlgorithmName = SHA-256
  5. sha256Matcher.hashIterations=1
  6. # base64 encoding
  7. sha256Matcher.storedCredentialsHexEncoded = false
  8.  
  9. #datasource type
  10. ds = org.apache.shiro.jndi.JndiObjectFactory
  11.  
  12. #datasourcename
  13. ds.resourceName = cfresource
  14.  
  15. #datasourcetype
  16. ds.requiredType = javax.sql.DataSource
  17.  
  18.  
  19.  
  20.  
  21. #configuring jdbc realm
  22. jdbcRealm = com.connectifier.authc.realm.CustomJDBCRealm
  23. jdbcRealm.credentialsMatcher = $sha256Matcher
  24. jdbcRealm.dataSource=$ds
  25. jdbcRealm.userRolesQuery=select name from role where email = ? and isactive=1
  26. jdbcRealm.authenticationQuery=select hash, salt from user where email = ?
  27. jdbcRealm.permissionsLookupEnabled=false
  28. securityManager.realms = $jdbcRealm
  29. #login url
  30. authc.loginUrl = /
  31.  
  32. #page to redirected to after logout
  33. logout.redirectUrl = /
  34.  
  35. #page to where to land after login
  36. authc.successUrl = /
  37.  
  38. #username parameter name in the loginform
  39. authc.usernameParam = username
  40.  
  41. #password parameter name in the loginform
  42. authc.passwordParam = password
  43.  
  44. #rememberme parameter name in the loginform
  45. authc.rememberMeParam=rememberme
  46.  
  47. #cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
  48. #securityManager.cacheManager = $cacheManager
  49. #jdbcRealm.authenticationCachingEnabled = true
  50.  
  51. [urls]
  52. # The /login.jsp is not restricted to authenticated users (otherwise no one could log in!), but
  53. # the 'authc' filter must still be specified for it so it can process that url's
  54. # login submissions. It is 'smart' enough to allow those requests through as specified by the
  55. # shiro.loginUrl above.
  56.  
  57. /* = anon
  58.  
  59. package com.connectifier.authc.realm;
  60.  
  61. import java.sql.Connection;
  62. import java.sql.PreparedStatement;
  63. import java.sql.ResultSet;
  64. import java.sql.SQLException;
  65.  
  66. import org.apache.shiro.authc.AccountException;
  67. import org.apache.shiro.authc.AuthenticationException;
  68. import org.apache.shiro.authc.AuthenticationInfo;
  69. import org.apache.shiro.authc.AuthenticationToken;
  70. import org.apache.shiro.authc.SimpleAuthenticationInfo;
  71. import org.apache.shiro.authc.UnknownAccountException;
  72. import org.apache.shiro.authc.UsernamePasswordToken;
  73. import org.apache.shiro.codec.Base64;
  74. import org.apache.shiro.realm.jdbc.JdbcRealm;
  75. import org.apache.shiro.util.ByteSource;
  76. import org.apache.shiro.util.JdbcUtils;
  77. import org.apache.shiro.util.SimpleByteSource;
  78. import org.slf4j.Logger;
  79. import org.slf4j.LoggerFactory;
  80.  
  81. /**
  82. * @author kiranchowdhary
  83. *
  84. * Application specific JDBC realm. If required override methods of {@link JdbcRealm} to load users, roles and
  85. * permissions from database.
  86. *
  87. * Do not override configuration in code if it can be done via shiro.ini file.
  88. */
  89. public class CustomJDBCRealm extends JdbcRealm {
  90.  
  91. private static final Logger log = LoggerFactory.getLogger(JdbcRealm.class);
  92.  
  93. public CustomJDBCRealm() {
  94. super();
  95. setSaltStyle(SaltStyle.COLUMN);
  96. }
  97.  
  98. /**
  99. * overriding the method which is in JdbcRealm. If SaltStyle is COLUMN, then gets String salt value from database
  100. * and forms salt byte array of type {@link ByteSource} with decoded string salt value and sets it to salt value of
  101. * AuthenticationInfo.
  102. */
  103. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
  104.  
  105. UsernamePasswordToken upToken = (UsernamePasswordToken) token;
  106. String username = upToken.getUsername();
  107.  
  108. // Null username is invalid
  109. if (username == null) {
  110. throw new AccountException("Null usernames are not allowed by this realm.");
  111. }
  112.  
  113. Connection conn = null;
  114. SimpleAuthenticationInfo info = null;
  115. try {
  116. conn = dataSource.getConnection();
  117.  
  118. String password = null;
  119. String salt = null;
  120. switch (saltStyle) {
  121. case NO_SALT:
  122. case CRYPT:
  123. case EXTERNAL:
  124. return super.doGetAuthenticationInfo(token);
  125. case COLUMN:
  126. String[] queryResults = getPasswordForUser(conn, username);
  127. password = queryResults[0];
  128. salt = queryResults[1];
  129. break;
  130. }
  131.  
  132. if (password == null) {
  133. throw new UnknownAccountException("No account found for user [" + username + "]");
  134. }
  135.  
  136. info = new SimpleAuthenticationInfo(username, password.toCharArray(), getName());
  137.  
  138. if (salt != null) {
  139. info.setCredentialsSalt(new SimpleByteSource(Base64.decode(salt)));
  140. }
  141.  
  142. } catch (SQLException e) {
  143. final String message = "There was a SQL error while authenticating user [" + username + "]";
  144. if (log.isErrorEnabled()) {
  145. log.error(message, e);
  146. }
  147.  
  148. // Rethrow any SQL errors as an authentication exception
  149. throw new AuthenticationException(message, e);
  150. } finally {
  151. JdbcUtils.closeConnection(conn);
  152. }
  153.  
  154. return info;
  155. }
  156.  
  157. private String[] getPasswordForUser(Connection conn, String username) throws SQLException {
  158.  
  159. String[] result;
  160. boolean returningSeparatedSalt = false;
  161. switch (saltStyle) {
  162. case NO_SALT:
  163. case CRYPT:
  164. case EXTERNAL:
  165. result = new String[1];
  166. break;
  167. default:
  168. result = new String[2];
  169. returningSeparatedSalt = true;
  170. }
  171.  
  172. PreparedStatement ps = null;
  173. ResultSet rs = null;
  174. try {
  175. ps = conn.prepareStatement(authenticationQuery);
  176. ps.setString(1, username);
  177.  
  178. // Execute query
  179. rs = ps.executeQuery();
  180.  
  181. // Loop over results - although we are only expecting one result,
  182. // since usernames should be unique
  183. boolean foundResult = false;
  184. while (rs.next()) {
  185.  
  186. // Check to ensure only one row is processed
  187. if (foundResult) {
  188. throw new AuthenticationException("More than one user row found for user [" + username
  189. + "]. Usernames must be unique.");
  190. }
  191.  
  192. result[0] = rs.getString(1);
  193. if (returningSeparatedSalt) {
  194. result[1] = rs.getString(2);
  195. }
  196.  
  197. foundResult = true;
  198. }
  199. } finally {
  200. JdbcUtils.closeResultSet(rs);
  201. JdbcUtils.closeStatement(ps);
  202. }
  203.  
  204. return result;
  205. }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement