我正在使用 Spring Security 5.1.5.RELEASE 并尝试设置ALLOW FROM为X-Frame-Options
我使用WhiteListedAllowFromStrategy并将 URL 列表传递给白名单,尽管header发送的是X-Frame-Options: DENY
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
String permittedRoutes [] = {"/", "/register"};
http
.headers()
.frameOptions()
.disable()
.addHeaderWriter(new XFrameOptionsHeaderWriter(new WhiteListedAllowFromStrategy(Arrays.asList("https://google.com"))));
http
.authorizeRequests()
.antMatchers(permittedRoutes).permitAll()
.and()
.authorizeRequests()
.antMatchers("/**").authenticated()
.and()
.formLogin()
.loginPage("/")
.defaultSuccessUrl("/home", true)
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.permitAll()
.invalidateHttpSession(true)
.clearAuthentication(true)
.deleteCookies("JSESSIONID")
.logoutSuccessUrl("/?logout");
}
@Override
public void configure(WebSecurity web) {
web
.ignoring()
.antMatchers("/assets/**", "/css/**", "/images/**", "/js/**", "/fonts/**", …Run Code Online (Sandbox Code Playgroud) 如何获取过滤器以应用于根路径之外的每个请求(我想忽略的请求除外)?这是我的例子:
我有一个 Spring Security 过滤器,如下所示:
private static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.antMatcher("/**")
.addFilterBefore(new AuthenticationFilter(), basicAuthenticationFilter.class);
}
@Override
public void configure(WebSecurity web) {
web
.ignoring()
.requestMatchers(SecurityServletRequestMatchers.servletIgnoreAuthMatcher());
}
}
Run Code Online (Sandbox Code Playgroud)
该过滤器填充一个CustomApiToken包含所有标头信息的对象,并将其放入 Spring Security 上下文中SecurityContextHolder.getContext().setAuthentication(token),以便轻松访问请求控制器上的令牌。
我正在尝试将 Springfox 添加到项目中,这意味着我想禁用 UI 和 API 文档页面的过滤器。
我最初的尝试是在该方法中添加一个子句:
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.antMatcher("/**")
.addFilterBefore(new AuthenticationFilter(), BasicAuthenticationFilter.class);
http
.requestMatcher(SecurityServletRequestMatchers.servletIgnoreAuthMatcher())
.headers() //.servletIgnoreAuthMatchers has all the swagger urls also
.defaultsDisabled()
.disable() …Run Code Online (Sandbox Code Playgroud) 我正在按照旧的教程来实现 Spring Security。不幸的是,antMatchers在我的配置类中没有被识别为方法,因此在做了一些研究之后,我相信requestMatchers方法是它的等价物。然而,未经身份验证,路径 ( /) 仍处于阻塞状态。我愿意允许这样做。
这是我的控制器:
package com.quadri.springsecurity.controllers;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quadri.springsecurity.models.Student;
@RestController
@RequestMapping("api/v1/students")
public class StudentController {
private static final List<Student> STUDENTS = Arrays.asList(
new Student(1, "James Bond"),
new Student(2, "Maria Jones"),
new Student(3, "Anna Smith")
);
@GetMapping(path = "{studentId}")
public Student getStudent(@PathVariable("studentId") Integer studentId) {
return STUDENTS.stream()
.filter(student -> studentId.equals(student.getStudentId()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("Student " + studentId + " does not …Run Code Online (Sandbox Code Playgroud) /* package whatever; // don't place package name! */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone {
public static void main (String[] args) throws java.lang.Exception {
try {
// your code goes here
SimpleDateFormat sdf= new SimpleDateFormat("dd-mm-yyyy");
Calendar cal= Calendar.getInstance();
String s = "12-12-2014";
Date dte=sdf.parse(s);
cal.setTime(dte);
System.out.println( cal.get(Calendar.WEEK_OF_YEAR)+"");
} catch (Exception e ) {} …Run Code Online (Sandbox Code Playgroud) 我使用Jersey原型创建了项目,由于无法解决以下依赖性,因此无法构建该项目。
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud) 我的应用程序可以有以下 URL:
/siteadmin/homepage/
/siteusers/customer/createCustomer
Run Code Online (Sandbox Code Playgroud)
以下是我的spring-security.xml:
/siteadmin/homepage/
/siteusers/customer/createCustomer
Run Code Online (Sandbox Code Playgroud)
如果我使用用户“a”登录并点击 URL http://localhost:8080/siteadmin/homepage/,则允许用户“a”查看页面,尽管他的角色不是admin。但是当我尝试点击时,http://localhost:8080/siteadminSpring Security 工作正常,即。它显示访问被拒绝页面。我想限制/admin/*没有Admin角色的用户的 URL 。
我有以下测试方法:
@RunWith(MockitoJUnitRunner.class)
public class AccountManagerTest {
@InjectMocks
private AccountManager accountManager = new AccountManagerImpl(null);
@Mock
private AuthStorage authStorage;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
/* REGISTER TESTS */
@Test
public void test_whenRegister_withAlreadyExistingEmail_thenDoNotRegister() throws AuthStorageException {
String email = "foo@bar.com";
String name = "Foo";
String password = "123456";
String password2 = "123456";
doThrow(new AuthStorageException("Email already in use")).when(authStorage).registerNewUser(Matchers.any());
assertFalse(accountManager.register(email, name, password, password2));
}
}
Run Code Online (Sandbox Code Playgroud)
测试以下类方法:
@Override
public Boolean register(String email, String name, String password, String password2) {
if (password.equals(password2)) {
try { …Run Code Online (Sandbox Code Playgroud) 我需要提供某些角色来访问以下格式的URL:
/connector/{programId}/order/{anything here}
Run Code Online (Sandbox Code Playgroud)
programId整数值在哪里,所以我尝试了以下操作,但它根本不起作用。
/connector/{programId}/order/{anything here}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用它**而不是programId零件时,它运行良好。但是,如何使它与pathVariable(始终为整数)一起使用。
这是我使用 Spring Boot 和 Spring Security 的代码。问题是当我曾经注销(使用Thyemleaf)时,注销对我不起作用。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username as principal, password as credentials,active from users where username=?")
.authoritiesByUsernameQuery("select username as principal,roles as role from users_roles where username=?")
.rolePrefix("ROLE_")
.passwordEncoder(new Md5PasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage("/login");
http
.authorizeRequests()
.antMatchers("/index1").permitAll();
http
.authorizeRequests()
.antMatchers("/user").hasRole("USER")
.and()
.logout();
http
.authorizeRequests()
.antMatchers("/adpage").hasRole("ADMIN");
http
.exceptionHandling().accessDeniedPage("/403"); …Run Code Online (Sandbox Code Playgroud) 我是Java的新手.
我似乎无法理解为什么这两个代码产生不同的输出.
请向我解释一下.
有什么区别y<=x;和y<=5;.正如你所看到的那样x是5,我不明白为什么我得到不同的输出.
for (int x = 0; x < 5; x++) {
for (int y = 1; y <=x ; y++) {
System.out.print("x");
}
for (int g = 4; g >= x; g--) {
System.out.print("*");
}
System.out.println();
}
Run Code Online (Sandbox Code Playgroud)
输出:
*****
x****
xx***
xxx**
xxxx*
Run Code Online (Sandbox Code Playgroud)
码:
for (int x = 0; x < 5; x++) {
for (int y = 1; y <= 5; y++) {
System.out.print("x");
}
for (int g = 4; …Run Code Online (Sandbox Code Playgroud) 我试图将a的输出传递ResultSet给Java HashMap.
Map<Integer, String> sIDpNumberHashMap = new HashMap<Integer, String>();
while (DBresult.next()) {
int sID = DBresult.getInt("slrid");
String pNumber = DBresult.getString("pNumber");
sIDpNumberHashMap.put(sID , pNumber );
System.out.println("Output1"+ sID + "\t" + pNumber + "\n");
}
System.out.println("Output2" + "\n" + sIDpNumberHashMap);
Run Code Online (Sandbox Code Playgroud)
而Output1显示所有记录(来自数据库).该put命令仅从ResultSet中获取最后一个值.
输出1:
502332262 101E2571G103
502332262 101E2571G103
502332262 116E3139P001
502332262 117E3640G025
502332262 314B7159G003
502332262 117E3640G025
Run Code Online (Sandbox Code Playgroud)
输出2:
{502332262=117E3640G025}
Run Code Online (Sandbox Code Playgroud)
如何使put命令迭代结果ResultSet?
我总是收到错误:
CREATE DATABASE bundesliga ERRORCODE 1007 CANT CREATE DATABASE bundesliga,数据库存在
这是我的代码:
CREATE DATABASE bundesliga;
DROP TABLE IF EXISTS Liga;
CREATE TABLE Liga (
);
DROP TABLE IF EXISTS Spiel;
CREATE TABLE Spiel ();
Run Code Online (Sandbox Code Playgroud) 我试图使用递归来查找数组中最大的数字,但没有得到我希望的结果。任何帮助将不胜感激。
public class ArrayMax {
public static int largestInteger(int[] array) {
return FindlargestInteger(array, 0, -99999999);
}
public static int FindlargestInteger(int[] array, int index, int max) {
if (index == array.length)
return max;
if (array[index] > max) {
max = array[index];
}
FindlargestInteger(array, index + 1, max);
return max;
}
}
Run Code Online (Sandbox Code Playgroud)