在带有 Spring Security 的 Spring MVC 中,是否可以实现这一点?
@Override WebSecurityConfigurerAdapter.configure(HttpSecurity)
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.authorizeRequests()
.mvcMatchers("/users/{authentication.principal.username}").hasAnyRole(ADMIN, MANAGER)
.antMatchers("/users/**").hasRole(ADMIN)
.anyRequest().authorized()
...
}
Run Code Online (Sandbox Code Playgroud)
/users/**是一个限制区域,只能由管理员访问。但是管理员应该仍然能够看到他们自己的个人资料 ( /users/user_with_manager_role),并且只能看到他们自己的个人资料,而不是任何其他用户的个人资料(无论他们的角色如何)。
我在安德鲁的回答中找到了解决方案。我的代码现在看起来像这样:
网络安全配置器适配器
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // added this annotation
public class SecurityConfig extends WebSecurityConfigurerAdapter
Run Code Online (Sandbox Code Playgroud)
@Override WebSecurityConfigurerAdapter.configure(HttpSecurity)
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.authorizeRequests()
// removed /users handling
.anyRequest().authorized()
...
}
Run Code Online (Sandbox Code Playgroud)
用户控制器
@Controller
@RequestMapping("/users")
public class UsersController
{
@GetMapping("{username}")
@PreAuthorize("authentication.principal.username == #username) || hasRole('ADMIN')")
public …Run Code Online (Sandbox Code Playgroud) 我的构建输出是out/production/classes. Java 文件可以很好地编译成类并被放入out/production/classes/[packageName],但不会复制资源。据我所知,他们应该直接进入out/production/classes目录。
如果相关,我正在使用 Java 11、Spring Boot 和 Gradle。这是我的build.gradle
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'net.impfox'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
// hidden
}
Run Code Online (Sandbox Code Playgroud)
我的编译器设置:
我的资源没有被复制到输出目录的原因可能是什么,我该如何解决这个问题?
我有一个使用 JavaMail API 发送 HTML 邮件的任务。这是我的代码的一小部分:
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
try {
helper.setTo(recipients);
helper.setSubject("Simple mail template");
helper.setText("<html><body>Hi There</body><html>",html:true);
} catch (MessagingException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个任务来将 HTML 移动到一个单独的文件中,并创建一个类来读取该 HTML 模板并用它发送邮件。关于如何做到这一点的任何建议?
我有一个程序可以在 PDF 文件中创建文本字段,以便它可以用作表单。我希望将我在创建的文本字段中写入的文本居中。这怎么可能?我的代码目前如下所示:
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("Field " + j + " " + i);
defaultAppearanceString = "/Helv 8 Tf 0 g"; //Textsize: 8
textBox.setDefaultAppearance(defaultAppearanceString);
acroForm.getFields().add(textBox);
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(inputField.getX(), inputField.getY(), inputField.getWidth(), inputField.getHeight());
widget.setRectangle(rect);
widget.setPage(page);
widget.setPrinted(true);
page.getAnnotations().add(widget);
Run Code Online (Sandbox Code Playgroud)
我想到了一个简单的函数来对齐文本,如下所示:
textBox.setAlignment(Alignment.CENTER);
Run Code Online (Sandbox Code Playgroud)
但我没有找到。
他们为什么不是关键词?这些是什么?
true, false, null
Run Code Online (Sandbox Code Playgroud)
更新快速解答
这些是保留字,但它们不是关键字.
通过规范验证的小技术区别 - ES3和ES5
我从一个 .xls 文件中捕获我的单元格,如下所示:
cell.getStringCellValue();
Run Code Online (Sandbox Code Playgroud)
但由于某些单元格是数字,我必须这样做:
try
{
cells[colIx] = cell.getStringCellValue();
} catch (IllegalStateException e)
{
cells[colIx] = String.valueOf(cell.getNumericCellValue());
}
Run Code Online (Sandbox Code Playgroud)
因为它得到一个 double 然后将它转换为一个字符串,这会导致一些不需要的操作,例如:
如何解决这个问题并获得实际的单元格值而不是一些转换后的数字?此外,所有单元格在excel中都定义为默认值
我想分割一个字符串,只要它包含字符" - "或它是否包含空格,但只有在" - "或空格之前和之后存在一个字符.
例:
" test" -> Dont split
"-test" -> Dont split
"test test" -> split
" test test" -> split
"test-" -> dont split
"test-test" -> split
"a-test" -> split
Run Code Online (Sandbox Code Playgroud) 当我向我的网址发送帖子请求时,我的控制器设法将记录保存到数据库,但我的ajax出错了.我究竟做错了什么?我的js代码:
function salvarValores(){
capturarValores()
$.ajax({
type: "POST",
url: "/service/newService/service",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify(( { 'cpfPessoa': cpfCliente, "descricaoServico": descricao } )),
success: function(data){
alert("Record successfully entered");
location.reload();
},
error: function(data){
alert("Error performing operation");
location.reload();
}
});
Run Code Online (Sandbox Code Playgroud)
}
我的春季启动控制器
@PostMapping("/service/newService/service")
@ResponseBody
public ResponseEntity<String> newService(@RequestBody Service service) {
if (serviceDao.addObject(service)) {
logger.debug("Adding data");
return new ResponseEntity<String>("Data successfully saved", HttpStatus.OK);
}
logger.error("Error to insert data in database");
return new ResponseEntity<String>("Error to insert data in database", HttpStatus.FAILED_DEPENDENCY);
}
Run Code Online (Sandbox Code Playgroud)
当我单击保存按钮时,我有一条警告,其中显示"错误执行操作"但该记录已正确插入数据库
java ×7
javascript ×2
apache ×1
apache-poi ×1
build ×1
email ×1
excel ×1
gradle ×1
html-email ×1
jakarta-mail ×1
java-11 ×1
jquery ×1
keyword ×1
pdf ×1
pdfbox ×1
regex ×1
split ×1
spring ×1
spring-boot ×1
spring-mvc ×1
textbox ×1