Java 弹簧靴&x2B;Oauth2+;Jwt
我正在尝试创建一个Spring引导应用程序,其中管理员需要使用表单登录[默认Spring表单登录]进行不同的登录,客户需要通过Angular应用程序登录。客户可以登录本地和社交网站[谷歌]。谁能给我推荐一份文件吗 我不需要Java 弹簧靴&x2B;Oauth2+;Jwt,java,spring-boot,spring-security,oauth-2.0,jwt,Java,Spring Boot,Spring Security,Oauth 2.0,Jwt,我正在尝试创建一个Spring引导应用程序,其中管理员需要使用表单登录[默认Spring表单登录]进行不同的登录,客户需要通过Angular应用程序登录。客户可以登录本地和社交网站[谷歌]。谁能给我推荐一份文件吗 我不需要sso,因为我不需要第三方登录 我在网上浏览了2天,找不到解决方案。以下内容将允许您通过表单登录或Github Oauth2登录使用登录 @Configuration @EnableWebSecurity public class WebSecurityConfig exten
sso
,因为我不需要第三方登录
我在网上浏览了2天,找不到解决方案。以下内容将允许您通过表单登录或Github Oauth2登录使用登录
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login/authenticate")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.deleteCookies("JSESSIONID")
.and().
oauth2Login().
loginPage("/login");;
}
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}
src/main/resources/templates/login.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
Invalid username and password.
</div>
<div th:if="${param.logout}">
You have been logged out.
</div>
<form th:action="@{/login/authenticate}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
<a href="/oauth2/authorization/github">Click here to login via Github</a>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>This is a secured page. Hello World!</title>
</head>
<body>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign Out"/>
</form>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example</title>
</head>
<body>
<h1>Welcome! This is not a secured page</h1>
<p>Click <a th:href="@{/hello}">here</a> to see a secured page</p>
</body>
</html>
src/main/resources/templates/home.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
Invalid username and password.
</div>
<div th:if="${param.logout}">
You have been logged out.
</div>
<form th:action="@{/login/authenticate}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
<a href="/oauth2/authorization/github">Click here to login via Github</a>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>This is a secured page. Hello World!</title>
</head>
<body>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign Out"/>
</form>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example</title>
</head>
<body>
<h1>Welcome! This is not a secured page</h1>
<p>Click <a th:href="@{/hello}">here</a> to see a secured page</p>
</body>
</html>
application.yaml
spring:
security:
oauth2:
client:
registration:
github:
client-id: your-github-client-id
client-secret: your-github-client-secret
注:
您必须生成github客户端和secret,如以下参考中所述
这些是被上述配置链接的过滤器,可以在过滤器5、6、7、12和13中设置断点
- 尝试访问不安全的url
,看看会发生什么/home
- 尝试访问受保护的url
,看看会发生什么/hello
- 尝试访问
url,看看会发生什么login
- 尝试从提交
,看看会发生什么登录
- 单击
链接,查看发生了什么通过Github登录
为了理解您的问题,让我们简化一下。假设一组用户有一个登录url和登录页面,其他用户有另一个登录url和不同的登录页面。所以,当未经验证的用户试图访问受保护的url时,系统需要重定向到登录页面。因为它是未经验证的用户,所以系统不知道是重定向到第一个登录页面还是第二个登录页面。所以,如果你能告诉我,你计划如何合理地解决这个问题,那么我也许能指出一些实现或解决方法document@KavithakaranKanapathippillai,谢谢你的帮助,实际上我有一个具有不同用户角色[管理员,用户]的数据库。Spring boot在此应用程序中用作后端。“角度”用于前端部分。与此同时,管理面板也集成在Leaf中。所以在我的例子中,用户应该能够使用用户名和密码或社交登录[JWT和Oauth2]从angular应用程序登录。对于adminpanel,我们需要一个单独的表单登录[基本spring登录表单]。你能帮我解决这个问题吗。请给我建议一个方法。提前感谢angular应用程序,登录屏幕会是这样吗(以匿名方式打开)?@KavithakaranKanapathippillai是的,如果我们转到
domain.com/admin/login
我们会得到另一个基本表单登录。这是一个管理员登录页面。管理员不需要angular应用程序。只允许用户。请告诉我进展如何。如果你能让它正常工作,我可以帮你安装面板