Skip to content
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

refactor: refactor oauth2 login #1169

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Datart
* <p>
* Copyright 2021
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package datart.server.config;

import datart.core.base.consts.Const;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@Slf4j
@Component
public class ExternalRegisterRedirectStrategy {

private static final String redirectUrl = "/auth";

public void redirect(HttpServletRequest request, HttpServletResponse response, String token) throws Exception {
String target = redirectUrl + "?token=" + URLEncoder.encode(token, StandardCharsets.UTF_8.name());
response.setHeader(Const.TOKEN, token);
response.sendRedirect(target);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Datart
* <p>
* Copyright 2021
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package datart.server.config;

import datart.core.base.exception.Exceptions;
import datart.server.service.ExternalRegisterService;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class Oauth2AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

private final ExternalRegisterService externalRegisterService;

private final ExternalRegisterRedirectStrategy registerRedirectStrategy;

public Oauth2AuthenticationSuccessHandler(ExternalRegisterService externalRegisterService,
ExternalRegisterRedirectStrategy registerRedirectStrategy) {
this.externalRegisterService = externalRegisterService;
this.registerRedirectStrategy = registerRedirectStrategy;
}

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
try {
String token = externalRegisterService.oauth2Register((OAuth2AuthenticationToken) authentication);
registerRedirectStrategy.redirect(request, response, token);
} catch (Exception e) {
Exceptions.e(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

private OAuth2ClientProperties oAuth2ClientProperties;

private Oauth2AuthenticationSuccessHandler authenticationSuccessHandler;

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(getApiPrefix() + "/tpa");
Expand All @@ -32,11 +34,18 @@ protected void configure(HttpSecurity http) throws Exception {
.and().oauth2Login().loginPage("/")
.and().logout().logoutUrl("/tpa/oauth2/logout").permitAll();
}

http.oauth2Login().successHandler(authenticationSuccessHandler);

}

@Autowired(required = false)
public void setoAuth2ClientProperties(OAuth2ClientProperties properties) {
this.oAuth2ClientProperties = properties;
}

@Autowired
public void setAuthenticationSuccessHandler(Oauth2AuthenticationSuccessHandler authenticationSuccessHandler) {
this.authenticationSuccessHandler = authenticationSuccessHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void shareChart(HttpServletRequest request, HttpServletResponse response)
@SkipLogin
@GetMapping(value = "shareDashboard/**")
public void shareDashboard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/shareChart.html").forward(request, response);
request.getRequestDispatcher("/shareDashboard.html").forward(request, response);
}

@SkipLogin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public String oauth2Register(OAuth2AuthenticationToken oauthAuthToken) throws Me

User user = userMapper.selectByNameOrEmail(oauthUser.getName());
if (user != null) {
return null;
PasswordToken passwordToken = new PasswordToken(user.getUsername(),
null,
System.currentTimeMillis());
return JwtUtils.toJwtString(passwordToken);
}

String emailMapping = getProperty(String.format("spring.security.oauth2.client.provider.%s.userMapping.email", oauthAuthToken.getAuthorizedClientRegistrationId()));
Expand Down