小编Pau*_*kin的帖子

Spring RestTemplate解析自定义错误响应

给定REST服务调用

http://acme.com/app/widget/123

收益:

<widget>
  <id>123</id>
  <name>Foo</name>
  <manufacturer>Acme</manufacturer>
</widget>
Run Code Online (Sandbox Code Playgroud)

此客户端代码有效:

RestTemplate restTemplate = new RestTemplate();
XStreamMarshaller xStreamMarshaller = new XStreamMarshaller();
xStreamMarshaller.getXStream().processAnnotations(
    new Class[] { 
        Widget.class,
        ErrorMessage.class
    });

HttpMessageConverter<?> marshallingConverter = new MarshallingHttpMessageConverter(
    xStreamMarshaller, xStreamMarshaller);

List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
    converters.add(marshallingConverter);

restTemplate.setMessageConverters(converters);

Widget w = restTemplate.getForObject(
    "http://acme.com/app/widget/{id}", Widget.class, 123L);
Run Code Online (Sandbox Code Playgroud)

但是,调用http://acme.com/app/widget/456会返回:

<error>
    <message>Widget 456 does not exist</message>
    <timestamp>Wed, 12 Mar 2014 10:34:37 GMT</timestamp>
</error>
Run Code Online (Sandbox Code Playgroud)

但是此客户端代码抛出异常:

Widget w = restTemplate.getForObject(
    "http://acme.com/app/widget/{id}", Widget.class, 456L);

org.springframework.web.client.HttpClientErrorException: 404 Not Found
Run Code Online (Sandbox Code Playgroud)

我试过了:

try {
    Widget w …
Run Code Online (Sandbox Code Playgroud)

rest spring-mvc

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

Spring SAML扩展和Spring Security CSRF保护冲突

我们有一个带有Spring Security(3.2.4)的Spring MVC(4.0.5)应用程序,它包含CSRF保护,工作正常.我们现在正在添加SAML安全扩展(spring-security-saml2-core 1.0.0),这会导致CSRF保护问题.

已在SSOCircle上配置元数据并尝试访问http://localhost:8080/myapp指向SSOCircle上的登录页面.身份验证后,浏览器会重定向到http://localhost:8080/myapp/saml/SSO并生成错误:

HTTP状态403 - 未找到预期的CSRF令牌.你的会话已经过期了吗?

如果我们关闭CSRF保护,一切正常.我们如何维护CSRF保护并仍然使用SAML扩展?

在设置SAML扩展之前,我们使用了登录表单并且CSRF保护工作,我们没有收到登录JSP上的错误,也没有令牌.

SAML之前的代码:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.authorizeRequests()
            .antMatchers("/login", "/login.request", "/logout").permitAll()
            .anyRequest()
                .hasAnyAuthority("MyRole")
                    .and().formLogin()
            .loginPage("/login.request").loginProcessingUrl("/login")
            .failureUrl("/login.request?error").permitAll().and().logout()
            .logoutUrl("/logout").permitAll()
            .logoutSuccessUrl("/login.request");
}
Run Code Online (Sandbox Code Playgroud)

SAML代码:

@Override
protected void configure(HttpSecurity http) throws Exception {
    //http.csrf().disable();

    http.httpBasic().authenticationEntryPoint(samlEntryPoint());

    http.addFilterBefore(metadataGeneratorFilter(),
            ChannelProcessingFilter.class).addFilterAfter(samlFilter(),
            BasicAuthenticationFilter.class);

    http
        .authorizeRequests()
            .antMatchers("/error").permitAll()
            .antMatchers("/saml/**").permitAll()
            .anyRequest()
                .hasAnyAuthority("MyRole")
            .anyRequest().authenticated();

    http.logout().logoutSuccessUrl("/");
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

重新启用CSRF保护并将日志记录设置为DEBUG后,以下是成功验证后立即出现的日志:

22.10.2014 16:54:17.374 [http-bio-8080-exec-8] DEBUG o.s.w.m.support.MultipartFilter -
                Using MultipartResolver 'filterMultipartResolver' for MultipartFilter

22.10.2014 16:54:17.377 [http-bio-8080-exec-8] DEBUG o.s.b.f.s.DefaultListableBeanFactory -
                Returning cached instance …
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-security spring-saml

9
推荐指数
3
解决办法
5754
查看次数

Eclipse Luna快速搜索命令

快速搜索命令似乎在Luna中消失了.在Keplar中,默认绑定为Ctrl+ Shift+ L.在Luna中,Ctrl+ Shift+ L显示当前键绑定的列表,这是Keplar之前的行为.我不介意将快速搜索绑定到另一个组合键,但它在键首选项中根本没有列出.这已被弃用了吗?是否可以使用不同的命令名称?

这是快速搜索在Keplar中的样子.窗口上的标题显示"快速文本搜索",但Keys首选项中的命令是"快速搜索命令".

Eclipse快速搜索

eclipse keyboard-shortcuts

8
推荐指数
1
解决办法
6527
查看次数

避免Hibernate LazyInitializationExceptions的策略

除了使用OpenSessionInView模式之外,还有其他方法可以避免Hibernate Web应用程序中的LazyInitializationExceptions吗?使用OpenSessionInView有任何缺点吗?

java database hibernate

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

Log4j登录到共享日志文件

有没有办法将log4j日志记录事件写入也由其他应用程序写入的日志文件.其他应用程序可能是非Java应用程序.有什么缺点?锁定问题?格式化?

java log4j

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

记录Web应用程序流/ Iteraction

什么工件/图表用于记录Web应用程序的流程,同时考虑静态页面之间的链接以及动态视图组件(html表单,JSP,Ajax等)如何与服务器端组件交互(Servlet,Struts操作等) ?使用UML图表?

architecture documentation diagram uml web-applications

7
推荐指数
1
解决办法
3278
查看次数

HashMap更新ArrayList

我刚开始学习使用HashMap并阅读java教程,但我遇到了麻烦.

我正在尝试更新HashMap中的List,但是我想获取该密钥的列表,是否有办法更新密钥的特定列表而不必制作... 5个不同的列表并更新它们?

 HashMap<String, ArrayList<String>> mMap = new HashMap<String, ArrayList<String>>();
        ArrayList<String> list = new ArrayList<String>();
        mMap.put("A", list);
        mMap.put("B", list);
        mMap.put("C", list);
        mMap.put("D", list);

        Iterator iter = mMap.entrySet().iterator();

        if (mMap.containsKey("A"))
        {   
            Map.Entry mEntry = (Map.Entry) iter.next();
            list.add("test");
            mMap.put("A",list);
            System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
        }
        else if (mMap.containsKey("B"))
        {   
            Map.Entry mEntry = (Map.Entry) iter.next();
            list.add("entry");
            mMap.put("B",list);
            System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
        }
Run Code Online (Sandbox Code Playgroud)

java arraylist hashmap

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

通过Spring-Kafka列出Kafka主题

我们想通过spring-kafka列出所有Kafka主题,以获得类似于kafka命令的结果:

bin/kafka-topics.sh --list --zookeeper localhost:2181
Run Code Online (Sandbox Code Playgroud)

在下面的服务中运行getTopics()方法时,我们得到org.apache.kafka.common.errors.TimeoutException:获取主题元数据时超时已到期

组态:

@EnableKafka
@Configuration
public class KafkaConfig {
    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:2181");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, 
            StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, 
            StringDeserializer.class);
        return new DefaultKafkaConsumerFactory<>(props);
}
Run Code Online (Sandbox Code Playgroud)

服务:

@Service
public class TopicServiceKafkaImpl implements TopicService {
    @Autowired
    private ConsumerFactory<String, String> consumerFactory;

    @Override
    public Set<String> getTopics() {
        try (Consumer<String, String> consumer = 
            consumerFactory.createConsumer()) {
            Map<String, List<PartitionInfo>> map = consumer.listTopics();
            return map.keySet();
    }
}
Run Code Online (Sandbox Code Playgroud)

Kafka已启动并正在运行,我们可以成功地将应用程序中的消息发送到主题.

java apache-kafka kafka-consumer-api spring-kafka

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

用于拆分长字符串的Eclipse快捷方式

我发誓我见过有人这样做,但我无法在各种快捷方式列表中找到它.

鉴于:

String s = "A very long ............................ String";
Run Code Online (Sandbox Code Playgroud)

是否有Eclipse快捷方式将其转换为:

String s = "A very long ............................ " 
    + "String";
Run Code Online (Sandbox Code Playgroud)

eclipse keyboard-shortcuts editor

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

Siteminder的Spring Security Java配置

我有一个inMemoryAuthentication配置工作:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(
            AuthenticationManagerBuilder authenticationManagerBuilder)
            throws Exception {

        authenticationManagerBuilder //
            .inMemoryAuthentication() //
                .withUser("employee") //
                    .password("employee") //
                    .roles("RoleEmployee")
        ;

    }

    @Override
    public void configure(WebSecurity webSecurity) throws Exception {
        webSecurity.ignoring().antMatchers("/resources/**");
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        // @formatter:off

        httpSecurity
            .authorizeRequests()
                .antMatchers("/login","/login.request","/logout").permitAll()
                .anyRequest().hasRole("RoleEmployee")
        .and()
            .formLogin()
                .loginPage("/login.request")
                .loginProcessingUrl("/login")
                .failureUrl("/login.request?error")
                .permitAll()
        .and()
            .logout()
                .logoutUrl("/logout")
                .permitAll()
                .logoutSuccessUrl("/login.request")
        ;

        // @formatter:on
    }
}
Run Code Online (Sandbox Code Playgroud)

我想现在使用Siteminder身份验证并将其更改为:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    private UserDetailsService …
Run Code Online (Sandbox Code Playgroud)

siteminder spring-mvc spring-security spring-java-config

6
推荐指数
1
解决办法
7878
查看次数