Shiro Pull Request 914

https stash.corp.netflix.com projects cme repos shiro pull-requests 914
https stash.corp.netflix.com projects cme repos shiro pull-requests 914

https://stash.corp.netflix.com/projects/CME/repos/shiro/pull-requests/914

Synopsis

This particular pull request gives support for OAuth2 bearer tokens for you to the Shiro authentication framework. This permits Shiro to end up being used with OAuth2-based applications, such because those that employ Google or Fb for authentication.

Changes

The using changes were built to implement this kind of feature:

  • A innovative OAuth2Token school was added to represent an OAuth2 bearer token.
  • The ShiroFilter class was basically modified to assist OAuth2 bearer symbol authentication.
  • A new OAuth2Realm class was basically added to provide the necessary authentication logic.
  • A new OAuth2Filter class seemed to be added to provide a simple way to filter needs that require OAuth2 bearer token authentication.

Benefits

This feature supplies the following benefits:

  • Allows Shiro to be able to be used with OAuth2-based applications.
  • Simplifies this process of authenticating users in OAuth2-based applications.
  • Provides a a great deal more secure way to be able to authenticate users found in OAuth2-based applications.

Usage

To be able to use this characteristic, you must initial add the following dependency to your current project:

 < dependency> < groupId> org. apache. shiro< /groupId> < artifactId> shiro-oauth2< /artifactId> < version> 2. 0. 0-SNAPSHOT< /version> < /dependency> 

Once you have added the dependency, you can and then configure Shiro to use OAuth2 bearer token authentication. The following is a good example of how to do this:

 // Create a new ShiroFilter instance. ShiroFilter filter = new ShiroFilter(); // Set the logon URL. filter. setLoginUrl("/login"); // Set typically the success URL. filtration. setSuccessUrl("/home"); // Fixed the unauthorized WEB LINK. filter. setUnauthorizedUrl("/unauthorized"); // Create a brand-new OAuth2Realm instance. OAuth2Realm realm = fresh OAuth2Realm(); // Established the realm in the ShiroFilter instance. filter. setRealm(realm); // Add the OAuth2Filter to the ShiroFilter instance. filter. addFilter("oauth2", new OAuth2Filter()); // Set the ShiroFilter instance on the ServletContext. ServletContext servletContext = getServletContext(); servletContext. setAttribute(ShiroFilter. FILTER_NAME, filter); 

Once you possess configured Shiro for you to use OAuth2 bearer token authentication, you can then make use of the OAuth2Token class to represent OAuth2 bearer tokens. The following is an example of how to do this:

 // Create a new OAuth2Token instance. OAuth2Token symbol = new OAuth2Token("accessToken"); // Set this token on typically the SecurityContext. SecurityContext securityContext = SecurityUtils. getSubject(). getSession(); securityContext. setAuthentication(new OAuth2Authentication(token)); 

Conclusion

This feature adds support intended for OAuth2 bearer tokens to the Shiro authentication framework. This kind of allows Shiro to be used together with OAuth2-based applications, these kinds of as those the fact that use Google or Facebook for authentication.