我有使用方法级安全性保护的工作应用程序:
RestController:
@PreAuthorize("hasPermission(#product, 'WRITE')")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Product save(@RequestBody Product product) {
return productService.save(product);
}
Run Code Online (Sandbox Code Playgroud)
PermissionEvaluator:
public class SecurityPermissionEvaluator implements PermissionEvaluator {
private Logger log = LoggerFactory.getLogger(SecurityPermissionEvaluator.class);
private final PermissionService permissionService;
public SecurityPermissionEvaluator(PermissionService permissionService) {
this.permissionService = permissionService;
}
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
return permissionService.isAuthorized(userDetails.getUser(), targetDomainObject, permission.toString());
}
@Override
public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
// almost the same implementation …Run Code Online (Sandbox Code Playgroud) 我无法理解@Resource注释的含义。我查看了在线资源,但它们似乎以难以理解的方式解释了相同的内容。@Resource如果可能的话,有人可以以简单的方式解释 的含义吗?
谢谢 !
Path p1 = Paths.get("/Users/jack/Documents/text1.txt");
Path p2 = Paths.get("/Users/jack/text2.txt");
Path result1 = p1.resolve(p2);
Path result2 = p1.relativize(p2);
System.out.println("result1: "+result1);
System.out.println("result2: "+result2);
Run Code Online (Sandbox Code Playgroud)
OUTPUT
result1: /Users/jack/text2.txt
result2: ../../text2.txt
Run Code Online (Sandbox Code Playgroud)
我不知道如何resolve()和relativize()作品?
什么是result1和result2?的实际用途?
我有 application.properties,它位于资源中
应用程序属性
hsm.provider=software
hsm.name=TestHsm
hsm.port=3001
hsm.ip=127.0.0.1
hsm.timeout=10000
Run Code Online (Sandbox Code Playgroud)
和控制器
@RestController
@RequestMapping("/hsm")
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行应用程序时,所有变量都保持为 NULL。我错过了什么?
编辑 这是 src 文件夹的项目结构
src
????main
? ????java
? ? ????com
? ? ????xyz
? ? ????hsmservice
? ? ????hsm
? ? ????api
? ? Application.java
? ? Controller.java
? ? HSM.java
? ?
? ????resources
? ? application.properties
? ? …Run Code Online (Sandbox Code Playgroud) 如何将Java中的范围(使用java.util.stream.LongStream或java.util.stream.IntStream)转换为Java 中的分隔字符串?
我试过了:
String str = LongStream.range(16, 30)
.boxed()
.map(String::valueOf)
.collect(Collectors.joining(","));
System.out.println(str);
Run Code Online (Sandbox Code Playgroud)
这打印:
16,17,18,19,20,21,22,23,24,25,26,27,28,29
Run Code Online (Sandbox Code Playgroud)
同样可以使用IntStream.是否有更方便的范围转换为分隔字符串?
我有一份订单清单,应按两个条件进行分组。
Order_Id| Customer | Date | Amount |
1 | "Sam" | 2019-03-21 | 100 |
2 | "Nick" | 2019-03-21 | 102 |
3 | "Dan" | 2019-03-21 | 300 |
4 | "Sam" | 2019-04-21 | 400 |
5 | "Jenny" | 2019-04-21 | 220 |
6 | "Jenny" | 2019-04-12 | 330 |
Run Code Online (Sandbox Code Playgroud)
对于当前示例,应该找到每月总金额最高的买家:
{
MARCH: { customer='Dan', amount=300 },
APRIL: { customer='Jenny', amount=550 }
}
Run Code Online (Sandbox Code Playgroud)
我能够找到一个解决方案:
public class Main {
public static void main(String[] args) { …Run Code Online (Sandbox Code Playgroud) 我有一些List<Map<String, Object>>需要使用Java 8 lambda表达式进行过滤的集合.我将收到带有标志的JSON对象,必须应用过滤条件.如果未收到JSON对象,则不需要过滤.
protected List<Map<String, Object>> populate(List<SomeObject> someObjects, String string) {
taskList.stream()
// How to put condition here? Ho to skip filter if no filter oprions are received?
.filter(someObject -> (if(string != null) someobject.getName == string))
// The second problem is to collect custom map like
.collect(Collectors.toMap("customField1"), someObject.getName()) ... // I need somehow put some additional custom fields here
}
Run Code Online (Sandbox Code Playgroud)
现在我正在收集这样的自定义地图:
Map<String, Object> someMap = new LinkedHashMap<>();
someMap.put("someCustomField1", someObject.field1());
someMap.put("someCustomField2", someObject.field2());
someMap.put("someCustomField3", someObject.field3());
Run Code Online (Sandbox Code Playgroud) 有一个任务是将@PathVariableSpring MVC中的文件路径传递给带有GET请求的REST服务.
我们可以POST通过在JSON中发送文件路径的String 来轻松完成.
我们如何处理GET请求并@Controller像这样?
@RequestMapping(value = "/getFile", method = RequestMethod.GET)
public File getFile(@PathVariable String path) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
请求:
GET /file/getFile/"/Users/user/someSourceFolder/8.jpeg"
Content-Type: application/json
Run Code Online (Sandbox Code Playgroud) 我想获取列值不等于null且列值不等于默认值的记录.我知道我们可以使用
SELECT * FROM table WHERE column_name is NOT NULL AND column_name != 'some_default_value';
Run Code Online (Sandbox Code Playgroud)
但如果将来有人改变表中的some_default_value怎么办?有灵活的解决方案吗?
如何按经度/纬度查询最近的 10 个对象(在我的情况下为 Distributors)。
每个分销商都有经度/纬度坐标。我将 Spring Data 和 Spring Data 用于简单查询,但据我所知,没有选项可以将 Spring Data 用于此类查询。
java ×8
java-8 ×3
java-stream ×3
spring ×3
rest ×2
spring-boot ×2
annotations ×1
filter ×1
hibernate ×1
jpa ×1
lambda ×1
mysql ×1
path ×1
resolve ×1
spring-data ×1
spring-mvc ×1
sql ×1