spring - Spring Boot 上的会话处理,对某些用户使用 LDAP 身份验证,对其他用户使用 LinkedIn API 登录
问题描述
我正在为人力资源部门开发一个基于 Java 的 Web 应用程序。有两种类型的用户:人力资源专家和申请人。专家通过 LDAP 身份验证登录,而申请人通过 LinkedIn API 登录。
如何配置这两种用户的会话?Spring有这个库吗?目前,我的项目没有会话配置,因此专家和申请人可以从同一台计算机同时登录。
此外,我还需要能够根据一次登录的用户类型对 .html 文件进行更改。例如:“申请!” 通过 LinkedIn 登录的用户应该可以看到按钮,通过 LDAP 登录的专家应该可以看到“查看申请人”按钮。如果您还可以告诉我 Thymeleaf、Spring 和您推荐给我的会话处理方法如何协同工作,那就太好了。
我的 LDAP 身份验证:
@EnableGlobalMethodSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().disable();
http.authorizeRequests().antMatchers("applicants**").fullyAuthenticated().and()
.authorizeRequests().antMatchers("**/job/**/applicants").fullyAuthenticated().and()
.formLogin().loginPage("/login").permitAll().and()
.logout().permitAll();
http.csrf().disable();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource(contextSource())
.passwordCompare()
.passwordEncoder(new LdapShaPasswordEncoder())
.passwordAttribute("userPassword");
}
@Bean
public DefaultSpringSecurityContextSource contextSource() {
return new DefaultSpringSecurityContextSource(Arrays.asList("ldap://localhost:8389/"), "dc=springframework,dc=org");
}
}
领英登录:
@RestController
@RequestMapping("/connect")
public class LinkedInController {
public static boolean connected = false;
public static Applicant applicant;
private static final String API_KEY = "xxxx";
private static final String SECRET_KEY = "xxxx";
private static final String REDIRECT_URI = "http://localhost:8080/connect/done/";
private static final String STATE = "xxxx";
private static final String NETWORK_NAME = "LinkedIn";
private static final String PROTECTED_RESOURCE_URL = "https://api.linkedin.com/v1/people/~:(%s)";
private static final String RESOURCE_FIELDS = "id,firstName,lastName,emailAddress,maiden-name,headline," +
"industry,summary,picture-url";
private OAuth20Service service;
private ApplicantService applicantService;
@Autowired
public LinkedInController(ApplicantService applicantService){
this.applicantService = applicantService;
}
@GetMapping()
public ModelAndView redirectToAuthorization (Model model) throws IOException, InterruptedException, ExecutionException {
// Replace these with your client id and secret
service = new ServiceBuilder(API_KEY)
.apiSecret(SECRET_KEY)
.scope("r_basicprofile r_emailaddress")
.callback(REDIRECT_URI)
.state(STATE)
.build(LinkedInApi20.instance());
final Scanner in = new Scanner(System.in);
final String authorizationUrl = service.getAuthorizationUrl();
System.out.println("Auth. link:" + authorizationUrl);
return new ModelAndView(new RedirectView(authorizationUrl));
}
@GetMapping("/done")
public ModelAndView getToken(@RequestParam("code") String code, @RequestParam("state") String state, Model model) throws IOException, InterruptedException, ExecutionException {
if (state.equals(STATE)){
connected = true;
System.out.println("State correct.");
final OAuth2AccessToken accessToken = service.getAccessToken(code);
final OAuthRequest request = new OAuthRequest(Verb.GET, String.format(PROTECTED_RESOURCE_URL, RESOURCE_FIELDS));
System.out.println(request.getUrl());
request.addHeader("x-li-format", "json");
request.addHeader("Accept-Language", "ru-RU");
service.signRequest(accessToken, request);
final Response response = service.execute(request);
ObjectMapper mapper = new ObjectMapper();
Applicant applicant = mapper.readValue(response.getBody(), Applicant.class);
this.applicant = applicant;
applicantService.persistNewApplicant(applicant);
}
model.addAttribute("success", LinkedInController.connected);
model.addAttribute("applicant", LinkedInController.applicant);
return new ModelAndView("connected.html");
}
}
我正在使用 Spring Boot、IntelliJ、H2 数据库、LinkedIn API 和 LDAP。
解决方案
推荐阅读
- typescript - 泛型函数的对象
- php - 如何使用 Google Calendar API quickstart.php 解决重定向 URI 错误
- jakarta-ee - Server Tomcat v9.0 Server at localhost 无法启动,eclipce j2ee
- python - 我是使用 TenserFlow 和 MNISt 数据库的深度神经网络的 pca,数据形状出现错误
- python - SQL 连接、where、有子句的问题
- scheme - 有人可以解释一下:(+2 (if (>ba) ba)) 吗?
- google-chrome - 我应该手动将 lastError 设置为 null 吗?
- python - 多个组合框列表排序Python(Tkinter)
- javascript - 如何解决“Router.use() 需要中间件函数但在 Function.use 处得到对象”错误
- r - 如何在 R 或 matlab 中从原始数据和查找表创建新表?