gpt4 book ai didi

java - Spring无法配置授权服务器

转载 作者:搜寻专家 更新时间:2023-11-01 03:47:24 25 4
gpt4 key购买 nike

我创建了一个简单的授权服务器,但无法配置它。

  1. 启动两个应用程序(8080 用于身份验证服务器,9999 用于客户端)。
  2. 转到 localhost:9999/client并被重定向到 localhost:8080/login (正如预期的那样)。
  3. 使用用户/用户填写登录表单。
  4. 重定向到 localhost:9999/client (正如预期的那样),但有 Hello, null而不是 Hello, user .

但是,如果我直接转到 localhost:8080/me , 我有 {"name":"user"} .如何检索 Hello, user

授权服务器

@RestController
@EnableAuthorizationServer
@SpringBootApplication
public class Application extends WebSecurityConfigurerAdapter {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@GetMapping({ "/user", "/me" })
public Map<String, String> user(Principal principal) {
return Collections.singletonMap("name", principal == null ? "null" : principal.getName());
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("user").authorities(AuthorityUtils.NO_AUTHORITIES);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin();
}
}

应用程序的属性

security:
oauth2:
client:
client-id: clientid
client-secret: clientsecret
scope: read,write
auto-approve-scopes: '.*'

客户端

@Configuration
@EnableAutoConfiguration
@EnableOAuth2Sso
@RestController
public class Client {

@GetMapping("/")
public String home(Principal principal) {
return "Hello, " + principal.getName();
}

public static void main(String[] args) {
new SpringApplicationBuilder(Client.class)
.properties("spring.config.name=client").run(args);
}

}

客户的属性

server:
port: 9999
context-path: /client
security:
oauth2:
client:
client-id: clientid
client-secret: clientsecret
access-token-uri: http://localhost:8080/oauth/token
user-authorization-uri: http://localhost:8080/oauth/authorize
resource:
user-info-uri: http://localhost:8080/me

更新:
我下载了a tutorial当一切正常时,但它有 ssoFilter这仅适用于 OAuth2 身份验证。我只想用 loginForm 配置它.
我也分享了一个临时的 example在 GitHub 上。我认为用它查找问题会更容易。

最佳答案

9999 8080 不同的端口会导致cross-origin HTTP request 当它从不同域请求资源时,或第一个资源本身服务的端口。

有关的更多详细信息HTTP access control (CORS)

spring官方网站上有泥煤样例Enabling Cross Origin Requests for a RESTful Web Service

我建议通过实现 Filter 接口(interface)在您的应用上执行 CORS 过滤器。

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {

public CorsFilter() {
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", "*"); //for production add only origins which should be allowed to access now for demo purposes this accepts all.
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); //i would reduce this method list if not all methods used this is added just for demo purposes
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, authorization");

if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, res);
}
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}
}

如果您使用的是 spring boot 应用程序,那么请务必包含您的新过滤器在组件扫描中创建的包

如果您使用“web.xml”进行配置:

然后添加过滤器

<filter>
<filter-name>CORS</filter-name>
<filter-class>com.mycompany.CorsFilter</filter-class>
</filter>

选项 A 在您的 servlet 上添加映射

<filter-mapping>
<filter-name>CORS</filter-name>
<servlet-name>MyServlet</servlet-name>
</filter-mapping>

选项B为所有应用添加过滤器:

<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern> <!--this will add cors on all apps-->
</filter-mapping>

关于java - Spring无法配置授权服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42867921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com