Spring Security <custom-filter>
标记的等效Java配置是什么?
<http>
<custom-filter position="FORM_LOGIN_FILTER" ref="myFilter"/>
</http>
Run Code Online (Sandbox Code Playgroud)
我试过了
http.addFilter( new MyUsernamePasswordAuthenticationFilter() )
Run Code Online (Sandbox Code Playgroud)
类扩展默认过滤器,但它始终使用默认过滤器formLogin
.
我的过滤器:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
public class MyUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter{
// proof of concept of how the http.addFilter() works
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
if (!request.getMethod().equals("POST")) {
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
}
System.out.println("running my own version of UsernmePasswordFilter ... ");
String username = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试对List <>对象进行排序,并且抛出此异常(仅适用于大型列表)
排序代码:
List<FinalSentence> sentenceList = finalRepresentation.getSentences();
Collections.sort(sentenceList); // <=== EXCEPTION THROWN HERE!!!
Run Code Online (Sandbox Code Playgroud)
FinalSentence类头:
public class FinalSentence implements Comparable<FinalSentence>{...}
Run Code Online (Sandbox Code Playgroud)
compareTo()实现:
@Override
public int compareTo(FinalSentence o) {
if (this == o) {
return 0;
}
if (this.score > o.score) {
return 1;
}
if (this.score < o.score) {
return -1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是例外:
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeHi(Unknown Source)
at java.util.ComparableTimSort.mergeAt(Unknown Source)
at java.util.ComparableTimSort.mergeCollapse(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source) …
Run Code Online (Sandbox Code Playgroud) 在使用新的Java 8 Stream
API时,我不禁要问,为什么不呢:
public interface Map<K,V> extends Function<K, V>
Run Code Online (Sandbox Code Playgroud)
甚至:
public interface Map<K,V> extends Function<K, V>, Predicate<K>
Run Code Online (Sandbox Code Playgroud)
使用以下default
方法实现相当容易Map
interface
:
@Override default boolean test(K k) {
return containsKey(k);
}
@Override default V apply(K k) {
return get(k);
}
Run Code Online (Sandbox Code Playgroud)
并且它允许Map
在map
方法中使用a :
final MyMagicMap<String, Integer> map = new MyMagicHashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
map.put("D", 4);
final Stream<String> strings = Arrays.stream(new String[]{"A", "B", "C", "D"});
final Stream<Integer> remapped = strings.map(map);
Run Code Online (Sandbox Code Playgroud)
或者作为Predicate
一种 …
我正在尝试使用Spring启动和Thymeleaf创建一个多语言应用程序.
我制作了几个属性文件来保存不同的消息,但我只能用我的浏览器语言显示它(我试过更改浏览器区域设置的扩展但它们似乎不起作用),无论如何我想在我的网站上放一个按钮做这个职责(改变语言),但我不知道如何或在哪里找到如何管理这个.
要告诉你我的配置:
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class I18nConfiguration extends WebMvcConfigurerAdapter {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("i18n/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
th:with="lang=${#locale.language}" th:lang="${lang}">
<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>Spring Boot and Thymeleaf</h3>
<p>Hello World!</p>
<p th:text="${nombre}"></p>
<h1 th:text="#{hello.world}">FooBar</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
messages_en_US.properties
hello.world = Hello people …
Run Code Online (Sandbox Code Playgroud) 我尝试通过将int基元更改为short来优化Android游戏的RAM使用率.在我这样做之前,我对Java中原始类型的性能感兴趣.
所以我使用caliper库创建了这个小测试基准.
public class BenchmarkTypes extends Benchmark {
@Param("10") private long testLong;
@Param("10") private int testInt;
@Param("10") private short testShort;
@Param("5000") private long resultLong = 5000;
@Param("5000") private int resultInt = 5000;
@Param("5000") private short resultShort = 5000;
@Override
protected void setUp() throws Exception {
Random rand = new Random();
testShort = (short) rand.nextInt(1000);
testInt = (int) testShort;
testLong = (long) testShort;
}
public long timeLong(int reps){
for(int i = 0; i < reps; i++){
resultLong += testLong;
resultLong -= …
Run Code Online (Sandbox Code Playgroud) 我想避免使application.properties
文件杂乱无章,比我认为单独存放文件更好。
application.properties
应该是这样的
@include module1.properties
@include module1.properties
...
###################################
######### Spring Misc #############
###################################
# Direct log to a log file
logging.file=/tmp/kmp-manager.log
#local listening port
server.port=8082
spring.profiles=nr_dev nr_testing production
spring.profiles.active=production
spring.datasource.platform=postgresql
java.security.egd=file:/dev/./urandom
Run Code Online (Sandbox Code Playgroud)
这完全有可能吗?如果没有,那么避免混乱的明智方法是什么?
鉴于我们现在有default
上的方法,interface
在Java中8,有没有我们可以访问实例方法从父类中的一个内(非任何方式static
)interface
,例如是这样的:
public class App {
int appConst = 3;
public interface MyInstanceInterface {
default int myAppConst() {
return appConst;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我interface
不是static
,因此它应该能够appConst
在App.this
上下文中访问.
此代码失败,出现以下编译错误:
错误:无法从静态上下文引用非静态变量appConst
为什么?
我想比较同一个数组的元素.这意味着我想要将0元素与其他所有元素进行比较,将1元素与其他所有元素进行比较,依此类推.问题是它没有按预期工作..我做的是我有两个for循环,从0到array.length-1 ..然后我有一个if语句如下:if(a [i]!= a [j + 1])
for (int i = 0; i < a.length - 1; i++) {
for (int k = 0; k < a.length - 1; k++) {
if (a[i] != a[k + 1]) {
System.out.println(a[i] + " not the same with " + a[k + 1] + "\n");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个字符串:
<https://gitlab.me.com/api/v3/projects/all?page=2&per_page=5>;
rel="next",
<https://gitlab.me.com/api/v3/projects/all?page=1&per_page=5>;
rel="first",
<https://gitlab.me.com/api/v3/projects/all?page=8&per_page=5>;
rel="last"
Run Code Online (Sandbox Code Playgroud)
所以格式是
(<val>; rel="key")*
Run Code Online (Sandbox Code Playgroud)
我想用以下格式将其解析为哈希:
next => https://gitlab.me.com/api/v3/projects/all?page=2&per_page=5
first => https://gitlab.me.com/api/v3/projects/all?page=1&per_page=5
last => https://gitlab.me.com/api/v3/projects/all?page=8&per_page=5
Run Code Online (Sandbox Code Playgroud)
在Java中,我将使用正则表达式模式来提取每个key => value对并将它们放入映射中.模式将是这样的:
<([^>]++)>;\s*rel="([^"]++)"
Run Code Online (Sandbox Code Playgroud)
哪个会给我第二个匹配组中的键和第一个匹配组中的值.同样的方法是实现这一目标的最好方法是Perl,还是我能做些什么?
PS我之所以使用Perl而不是Java的原因是服务器没有Java.
为什么不能让枚举在Java中扩展一些类?
以下示例显示:
E1
.E2
.例如:
public class Try_ExtendEnum {
public static enum E1 {
North,
East,
South,
West
}
public static enum E2 {
North(0),
Eash(90),
South(180),
West(270);
private double degrees;
E2(double degrees) {
this.degrees = degrees;
}
public double getDegrees() {
return degrees;
}
}
public static class DegreesMeasure {
private double degrees;
public DegreesMeasure(double degrees) {
this.degrees = degrees;
}
public double getDegrees() {
return degrees;
}
}
public static enum E3 extends …
Run Code Online (Sandbox Code Playgroud)