小编Ste*_*ane的帖子

Spring Data JPA Update @Query没有更新?

我有一个更新查询:

@Modifying
@Transactional
@Query("UPDATE Admin SET firstname = :firstname, lastname = :lastname, login = :login, superAdmin = :superAdmin, preferenceAdmin = :preferenceAdmin, address =  :address, zipCode = :zipCode, city = :city, country = :country, email = :email, profile = :profile, postLoginUrl = :postLoginUrl WHERE id = :id")
public void update(@Param("firstname") String firstname, @Param("lastname") String lastname, @Param("login") String login, @Param("superAdmin") boolean superAdmin, @Param("preferenceAdmin") boolean preferenceAdmin, @Param("address") String address, @Param("zipCode") String zipCode, @Param("city") String city, @Param("country") String country, @Param("email") String email, @Param("profile") String …
Run Code Online (Sandbox Code Playgroud)

java jpa spring-data

47
推荐指数
3
解决办法
12万
查看次数

使用JWT和Basic身份验证保护REST应用程序是否有意义?

我有一个Spring REST应用程序,它首先使用基本身份验证进行保护.

然后我添加了一个登录控制器,它创建了一个JWT JSON Web令牌,用于后续请求.

我可以将以下代码移出登录控制器并进入安全过滤器吗?然后我不再需要登录控制器了.

tokenAuthenticationService.addTokenToResponseHeader(responseHeaders, credentialsResource.getEmail());
Run Code Online (Sandbox Code Playgroud)

或者我可以删除基本身份验证吗?

将基本身份验证与JWT混合是一种好的设计吗?

虽然一切正常,但我在这里有点黑暗,以便最好地设计这种安全性.

spring-security basic-authentication jwt

37
推荐指数
1
解决办法
2万
查看次数

自己的Spring OAuth2服务器以及第三方OAuth提供商

在Spring Boot应用程序中,我有一个OAuth2授权/资源服务器.基于此和Spring Security,我已经获得了我的Spring MVC REST API端点.

除此之外,我还想基于第三方OAuth提供商(如Twitter,Facebook,Google)为我的REST端点添加身份验证.

在我的应用程序中,我有两个实体 - UserSocialUser.SocialUser代表社交网络中的用户个人资料 User可以有0-*关联SocialUsers.现在我可以在Twitter上验证用户,之后我在我的数据库中创建两条记录 - User和SocialUser.SocialUser包含Twitter发布的访问/刷新令牌以及来自此社交网络的一些其他配置文件信息.

现在我不知道如何将从社交网络创建的用户与我现有的身份验证\授权流程相关联.对于这个用户,我想创建自己的(由我自己的OAuth2授权服务器)accessToken并将其提供给客户端.

此外,该用户在其实体中没有用户名,密码和电子邮件User.而且我也不知道如何手动创建自己的访问令牌并将其发送到客户端以供将来的API调用.

我找到了一些例子:

@Inject
private TokenEndpoint tokenEndpoint;

public String createAccessToken(User user) {
    HashMap<String, String> parameters = new HashMap<String, String>();
    parameters.put("client_id", "appid");
    parameters.put("client_secret", "myOAuthSecret");
    parameters.put("grant_type", "password");
    parameters.put("password", user.getPassword());
    parameters.put("scope", "read write");
    parameters.put("username", user.getUsername());

    // principal ??
    return tokenEndpoint.getAccessToken(principal, parameters);
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何Principal基于我的User实体创建,我也不确定这是一个正确的方法.

那么,主要的问题是 - 如何通过我自己的OAuth服务器为这个新用户手动生成这个新令牌?

请告诉我如何才能正确实施.谢谢.

更新:

我已添加ProviderSignInController到我的应用程序中,现在能够通过Twitter执行完整的OAuth舞蹈.此外,我已经实现了自己的Neo4jConnectionRepository,Neo4jUsersConnectionRepository因为我使用Neo4j作为主数据库. …

spring-mvc spring-security oauth-2.0 spring-social spring-security-oauth2

21
推荐指数
1
解决办法
4329
查看次数

REST POST控制器说:无法读取JSON:由于输入结束而没有要映射的内容

我正在对REST控制器POST处理程序进行集成测试.好吧,我正在努力.

它给了我HttpMessageNotReadableException异常:无法读取JSON:由于输入结束没有要映射的内容

这是我的控制器:

@Controller
@RequestMapping("admin")
public class AdminController {

    private static Logger logger = LoggerFactory.getLogger(AdminController.class);

    private static final String TEMPLATE = "Hello, %s!";

    @Autowired 
    private AdminService adminService;

    @Autowired
    private AdminRepository adminRepository;

    @RequestMapping(value = "crud", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ResponseEntity<Admin> add(@RequestBody Admin admin, UriComponentsBuilder builder) {
        AdminCreatedEvent adminCreatedEvent = adminService.add(new CreateAdminEvent(admin.toEventAdmin()));
        Admin createdAdmin = Admin.fromEventAdmin(adminCreatedEvent.getEventAdmin());
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/json; charset=utf-8");
        responseHeaders.setLocation(builder.path("/admin/{id}").buildAndExpand(adminCreatedEvent.getAdminId()).toUri());
        return new ResponseEntity<Admin>(createdAdmin, responseHeaders, HttpStatus.CREATED);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public String handleException(HttpMessageNotReadableException e) …
Run Code Online (Sandbox Code Playgroud)

testing rest post json

18
推荐指数
2
解决办法
7万
查看次数

无法写入请求:找不到合适的HttpMessageConverter请求类型[org.json.JSONObject]和内容类型[application/json]

我正在尝试将具有JSON有效负载的POST请求发送到远程服务器.

这个GET curl命令工作正常:

curl -H "Accept:application/json" --user aaa@aaa.com:aaa "http://www.aaa.com:8080/aaa-project-rest/api/users/1" -i
Run Code Online (Sandbox Code Playgroud)

而这个POST也可以正常工作:

curl -H "Accept:application/json" -H "Content-Type: application/json" "http://www.aaa.com:8080/aaa-project-rest/api/users/login" -X POST -d "{ \"email\" : \"aaa@aaa.com\", \"password\" : \"aaa\" }" -i
Run Code Online (Sandbox Code Playgroud)

所以我试图在我的Android应用程序中模仿它.

该应用程序在第一个GET请求上正常工作,但在第二个POST请求上提供了400个错误请求.

以下是适用于GET请求的代码:

  RestTemplate restTemplate = new RestTemplate();
  restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
  HttpHeaders httpHeaders = Common.createAuthenticationHeaders("aaa@aaa.com" + ":" + "aaa");
  User user = null;
  ResponseEntity<User> responseEntity = restTemplate.exchange("http://" + REST_HOST + ":8080/aaa-project-rest/api/users/" + 1L, HttpMethod.GET, new HttpEntity<Object>(httpHeaders), User.class);
Run Code Online (Sandbox Code Playgroud)

以下是POST请求的源代码:

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
User user = null;
HttpHeaders headers …
Run Code Online (Sandbox Code Playgroud)

post android resttemplate

14
推荐指数
2
解决办法
5万
查看次数

无法登录Neo4j服务器

在新安装的neo4j-community-2.2.4上,我收到消息"用户名或密码无效".当使用默认用户名neo4j和密码neo4j在localhost:7474/browser上提交登录表单时.

我确实启用了org.neo4j.server.webserver.address = 0.0.0.0和dbms.security.auth_enabled = true,服务器停止并启动浏览器shift-reload.

然后我更改了属性org.neo4j.server.webserver.address = 127.0.0.1以匹配我的/ etc/hosts并尝试127.0.0.1:7474/browser但得到了相同的消息.

这是浏览器控制台输出:

Remote Address:127.0.0.1:7474
Request URL:http://localhost:7474/db/data/
Request Method:GET
Status Code:401 Unauthorized
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,fr;q=0.6,ru;q=0.4,es;q=0.2,sv;q=0.2,nb;q=0.2,et;q=0.2
Authorization:Basic bmVvNGo6bmVvNGo=
Cache-Control:no-cache
Connection:keep-alive
Cookie:languageCodeAdmin=en; PHPSESSID=vcbvfkvj3shajhlh5u2pue9i70; admin_template_phone_client=0; admin_template_touch_client=0; admin_template_model=1
Host:localhost:7474
If-Modified-Since:Wed, 11 Dec 2013 08:00:00 GMT
Pragma:no-cache
Referer:http://localhost:7474/browser/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.120 Chrome/37.0.2062.120 Safari/537.36
X-stream:true
Response Headersview source
Content-Length:135
Content-Type:application/json; charset=UTF-8
Date:Sat, 19 Sep 2015 09:14:03 GMT
Server:Jetty(9.2.4.v20141103)
WWW-Authenticate:None
{
  "errors" : [ {
    "message" : …
Run Code Online (Sandbox Code Playgroud)

neo4j

11
推荐指数
1
解决办法
6739
查看次数

为REST Spring HATEOAS控制器定义资源汇编程序

我正在尝试将HATEOAS链接添加到由Spring REST控制器提供的JSON资源.

我发现我应该使用https://github.com/spring-projects/spring-hateoas中描述的资源汇编程序

该示例显示Person类和PersonResource类.

我理解PersonResource类定义为:

public class PersonResource extends ResourceSupport {
}
Run Code Online (Sandbox Code Playgroud)

什么是Person类?它是数据域类吗?

在我的例子中,我已经定义了一个Admin类,它是一个REST域类,我将其指定为具有资源支持:

public class Admin extends ResourceSupport {

    private String firstname;
    private String lastname;
    private String email;
    private String login;
    private String password;
    private String passwordSalt;

    public Admin() {
    }

    public String getFirstname() {
        return this.firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return this.lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getEmail() { …
Run Code Online (Sandbox Code Playgroud)

rest spring-hateoas

10
推荐指数
1
解决办法
1万
查看次数

Maven编译给出:找不到符号 - 对于坐在同一个应用程序中的类

我遇到这样令人费解的问题已经有一段时间了.我有一个类引用另一个坐在同一个应用程序中的另一个包中的类,也就是说,不在另一个jar存档文件中.

包含类是learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java

包含的类是/home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/config/WebTestConfiguration.java

在Eclipse下,编辑器中没有问题,也没有编译错误.

但是运行Maven构建会产生编译错误:

mvn clean test-compile -Pacceptance

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project learnintouch-rest: Compilation failure: Compilation failure:
[ERROR] /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java:[16,46] cannot find symbol
[ERROR] symbol:   class WebTestConfiguration
[ERROR] location: package com.thalasoft.learnintouch.rest.config
[ERROR] /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java:[21,116] cannot find symbol
[ERROR] symbol: class WebTestConfiguration
Run Code Online (Sandbox Code Playgroud)

这是包含类的代码:

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@WebAppConfiguration
@ContextConfiguration(classes = {
    ApplicationConfiguration.class,
    WebSecurityConfig.class,
    WebConfiguration.class,
    WebTestConfiguration.class
})
public abstract class AbstractControllerTest {
Run Code Online (Sandbox Code Playgroud)

此抽象测试类位于验收测试目录下,该目录要求在运行Maven命令时显式激活-Pacceptance配置文件.

默认配置文件不运行此验收测试,而只执行某些集成测试.

需要注意的一点是,这个抽象类看起来像集成测试中使用的一个抽象类.

这是集成测试的包含类:

import com.thalasoft.learnintouch.rest.config.WebTestConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {ApplicationConfiguration.class},
        WebSecurityConfig.class,
        WebConfiguration.class,
        WebTestConfiguration.class
        })
@Transactional

public abstract …
Run Code Online (Sandbox Code Playgroud)

java eclipse spring maven

10
推荐指数
1
解决办法
5万
查看次数

如何在库项目中具有绝对导入路径?

我有一个带工作区的库,其中包含两个项目,一个用于库本身,一个用于测试应用程序。

??? projects
    ??? midi-app
    ??? midi-lib
Run Code Online (Sandbox Code Playgroud)

在工作空间tsconfig.json文件中,我配置了一些@app@lib路径:

"paths": {
  "@app/*": ["projects/midi-app/src/app/*"],
  "@lib/*": ["projects/midi-lib/src/lib/*"],
  "midi-lib": [
    "dist/midi-lib"
  ],
  "midi-lib/*": [
    "dist/midi-lib/*"
  ]
}
Run Code Online (Sandbox Code Playgroud)

在上述projects/midi-lib/tsconfig.lib.json文件上扩展了一个tsconfig.json文件:

"extends": "../../tsconfig.json",
Run Code Online (Sandbox Code Playgroud)

有一个public-api.ts文件包含:

export * from './lib/midi-lib.module';
Run Code Online (Sandbox Code Playgroud)

我可以将此库与测试应用程序一起使用。

但是,当我尝试在另一个客户端应用程序,另一个工作空间(作为节点模块导入)中使用它时,在未知路径上会出现很多错误 Can't resolve '@lib/...'

如何表达库路径,以便它们在客户端应用程序中公开?或者在打包库时如何转换库路径?

作为附带的问题,我想知道为什么扩展未按相反方式进行。为什么tsconfig.json文件没有在文件上扩展projects/midi-lib/tsconfig.lib.json

这是我打包然后使用库的方法:

要打包该库,请在父package.json文件的scripts数组中添加以下脚本

"copy-license": "cp ./LICENSE.md ./dist/midi-lib",
"copy-readme": "cp ./README.md ./dist/midi-lib",
"copy-files": "npm run copy-license && npm run copy-readme",
"build-lib": "ng build midi-lib",
"npm-pack": "cd …
Run Code Online (Sandbox Code Playgroud)

typescript tsconfig angular angular-library tsconfig-paths

10
推荐指数
1
解决办法
818
查看次数

吞下消息:错误:未捕获(在承诺中):[对象未定义]

我的登录组件在被一个有关promise中未定义对象的错误消息删除之前会暂时显示.

这是承诺定义:

  static init(): Promise<any> {
    KeycloakClientService.auth.loggedIn = false;
    return new Promise((resolve, reject) => {
      const keycloakConfig = {
      url: environment.KEYCLOAK_URL,
      realm: environment.KEYCLOAK_REALM,
      clientId: environment.KEYCLOAK_CLIENTID,
      'ssl-required': 'external',
      'public-client': true
      };
      const keycloakAuth: any = new Keycloak(keycloakConfig);

      keycloakAuth.init({onLoad: 'check-sso'})
        .success(() => {
          KeycloakClientService.auth.loggedIn = true;
          KeycloakClientService.auth.authz = keycloakAuth;
          KeycloakClientService.auth.logoutUrl = environment.KEYCLOAK_URL
          + '/realms/' + environment.KEYCLOAK_REALM + '/protocol/openid-connect/logout?redirect_uri='
          + document.baseURI;
          console.log('=======>> The keycloak client has been initiated successfully');
          resolve('Succeeded in initiating the keycloak client');
        })
        .error(() => {
          reject('Failed to initiate the …
Run Code Online (Sandbox Code Playgroud)

javascript promise angular-promise keycloak angular

9
推荐指数
1
解决办法
2620
查看次数