Skip to content

Add Generic AuthenticationFilter #7025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2019
Merged

Conversation

sbespalov
Copy link
Contributor

Fixes #6506

This PR contains AuthenticationWebFilter concept implementation for regular Spring Security Filter Chain:

  • AuthenticationConverter interface (aligned to ServerAuthenticationConverter)
  • GenericAuthenticationFilter implementation (aligned to AuthenticationWebFilter)
  • BasicAuthenticationConverter implementation (aligned to ServerHttpBasicAuthenticationConverter)

@rwinch the AbstractAuthenticationProcessingFilter.requiresAuthentication() method modified here to throw exceptions because these exceptions declared for AbstractAuthenticationProcessingFilter.unsuccessfulAuthentication method, which need to be called within GenericAuthenticationFilter.requiresAuthentication in case of AuthenticationConverter.convert decides to throw the AuthenticationException. So this seems necessary to have this change.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 20, 2019
@sbespalov
Copy link
Contributor Author

@rwinch can you please have a look at this PR?

Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @sbespalov!

I have added comments inline. Please also

  • add @since to each new class, interface, method
  • add tests for the changes

@rwinch rwinch added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 27, 2019
@rwinch rwinch self-assigned this Jun 27, 2019
@rwinch rwinch added in: web An issue in web modules (web, webmvc) type: enhancement A general enhancement labels Jun 27, 2019
@rwinch rwinch changed the title issue/6506: AuthenticationConverter implementation Add AuthenticationConverter Jun 27, 2019
@sbespalov
Copy link
Contributor Author

sbespalov commented Jul 5, 2019

@rwinch thanks for review

requested changes done

add @since to each new class, interface, method

done

add tests for the changes

done

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 5, 2019
Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates I have responded inline.

@sbespalov
Copy link
Contributor Author

@rwinch thanks for fast review, requested changes done

Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates. I just now noticed that we need to improve the package structure some. Please refer inline for the details.

* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.authentication.www;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is generic, the package should not be in the www package. It should be moved up a package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @author Sergey Bespalov
* @since 5.2.0
*/
public class GenericAuthenticationFilter extends OncePerRequestFilter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename to AuthenticationFilter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.authentication.www;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is generic, the package should not be in the www package. It should be moved up a package

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.authentication.www;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is generic, the package should not be in the www package. It should be moved up a package

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

public class GenericAuthenticationFilter extends OncePerRequestFilter {

private RequestMatcher requestMatcher = AnyRequestMatcher.INSTANCE;
private AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this will be moved up a package, we cannot use BasicAuthenticationConverter in this class otherwise we will have package tangles.

A few solutions that we could use:

  1. Add a new converter named FormLoginAuthenticationConverter in org.springframework.security.web.authentication that is the default value. This is my preferred approach. If we do this, then UsernamePasswordAuthenticationFilter would use FormLoginAuthenticationConverter.
  2. Require AuthenticationConverter to be injected. While this solution is a smaller change, I don't like that the user is required to inject the converter when we could reasonably default it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done with option 2.

For not to make this PR grows too much I can create a follow-up issue to provide FormLoginAuthenticationConverter as default.

private RequestMatcher requestMatcher = AnyRequestMatcher.INSTANCE;
private AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter();
private AuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
private AuthenticationFailureHandler failureHandler = new AuthenticationEntryPointFailureHandler(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this will be moved up a package, we cannot use BasicAuthenticationEntryPoint in this class otherwise we will have package tangles.

A few solutions that we could use:

  1. Use HttpStatusEntryPoint or LoginUrlAuthenticationEntryPoint. I think that the HttpStatusEntryPoint makes more sense since we don't know if there is anything in the app to handle the URL, but I am open to discussion on this.
  2. Require AuthenticationFailureHandler to be injected. While this solution is a smaller change, I don't like that the user is required to inject the converter when we could reasonably default it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done with HttpStatusEntryPoint as default.

@sbespalov sbespalov force-pushed the issue/6506 branch 2 times, most recently from dd5f83f to e32cc90 Compare July 17, 2019 02:21
@sbespalov
Copy link
Contributor Author

@rwinch packages fixed as you requested.

@rwinch rwinch added status: duplicate A duplicate of another issue and removed status: feedback-provided Feedback has been provided labels Jul 23, 2019
@rwinch rwinch added this to the 5.2.0.M4 milestone Jul 23, 2019
@rwinch rwinch merged commit f1187bd into spring-projects:master Jul 23, 2019
@rwinch rwinch changed the title Add AuthenticationConverter Add Generic AuthenticationFilter Jul 23, 2019
@rwinch
Copy link
Member

rwinch commented Jul 23, 2019

Thanks @sbespalov! This is now merged into master

rwinch added a commit that referenced this pull request Aug 2, 2019
This reverts to the old behavior from BasicAuthenticationFilter.
Specifically, if a token has an empty password, it still parses a username
and an empty String password.

Issue gh-7025
kostya05983 pushed a commit to kostya05983/spring-security that referenced this pull request Aug 26, 2019
This reverts to the old behavior from BasicAuthenticationFilter.
Specifically, if a token has an empty password, it still parses a username
and an empty String password.

Issue spring-projectsgh-7025
@carlspring
Copy link

Hi!

When could we expect this to be released and in what version?

@jzheaux
Copy link
Contributor

jzheaux commented Sep 9, 2019

@carlspring thanks for asking. You can see on the right-hand side of the description a section called "Milestones". This ticket was resolved for the 5.2.0.M4 milestone, so it's available there and later versions beyond that.

The next GA version is 5.2.0, and you can follow the release date via the Milestones page.

@carlspring
Copy link

Thanks for confirming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web An issue in web modules (web, webmvc) status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Generic AuthenticationFilter
5 participants