如果我的控制器的URL被正确保护,我试图弄清楚如何进行单元测试.以防有人改变现状并意外删除安全设置.
我的控制器方法如下所示:
@RequestMapping("/api/v1/resource/test")
@Secured("ROLE_USER")
public @ResonseBody String test() {
return "test";
}
Run Code Online (Sandbox Code Playgroud)
我设置了一个WebTestEnvironment,如下所示:
import javax.annotation.Resource;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({
"file:src/main/webapp/WEB-INF/spring/security.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/servlet-context.xml" })
public class WebappTestEnvironment2 {
@Resource
private FilterChainProxy springSecurityFilterChain;
@Autowired
@Qualifier("databaseUserService")
protected UserDetailsService userDetailsService;
@Autowired
private WebApplicationContext wac;
@Autowired …Run Code Online (Sandbox Code Playgroud) 我正在看一下Spring MVC提供的CharacterEncodingFilter.我想知道为什么只有在请求编码被强制为给定编码时才能设置响应编码?如果在接受标题字段中未指定任何内容,为什么不能设置默认响应编码?或者如果请求中没有编码?
代码:
@Override
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (this.encoding != null && (this.forceEncoding
|| request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(this.encoding);
if (this.forceEncoding) {
response.setCharacterEncoding(this.encoding);
}
}
filterChain.doFilter(request, response);
}
Run Code Online (Sandbox Code Playgroud)
我发现这是参考 https://jira.springsource.org/browse/SPR-3328?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel 说明响应编码只能在请求时设置编码被强制设置.为什么?
马丁,提前谢谢
我有一个 conda 环境,我通过运行将其作为内核提供给我的 Jupyter 实例:
python -m ipykernel install --user --name my-env-name --display-name "Python (my-env-name)"
在这个环境中,我想在 Jupyter 中使用 R,利用rpy2's%load_ext rpy2.ipython命令来启用%%R魔法。但是,rpy2使用的是我的全局 R,而不是安装在我的 conda 环境中的 R。我通过以下方式检查了我的 R 家:
%%R
R.home()
Run Code Online (Sandbox Code Playgroud)
(我还可以检查情况%run -m rpy2.situation在Jupyter笔记本(源),然而,这似乎被打破rpy2版本之间的某个地方3.1.0和3.2.1......至少对我来说,被扔UnboundLocalError: local variable 'rpy2' referenced before assignment在3.1.0和它工作3.2.1)。
如何让我的 Jupyter 笔记本从我的 conda 环境加载 R 安装?